Skip to content

Commit

Permalink
Merge pull request #461 from microsoft/release/update/240810122408
Browse files Browse the repository at this point in the history
Resync from Main branch on 240809
  • Loading branch information
MattB-msft authored Aug 10, 2024
2 parents 0bf4724 + c0cf11f commit be9ee13
Show file tree
Hide file tree
Showing 36 changed files with 3,328 additions and 224 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,14 @@ public T WithCorrelationId(Guid correlationId)
/// <returns></returns>
public T WithHeader(string key, string value)
{
_headers.Add(key, value);
if ( _headers.ContainsKey(key))
{
_headers[key] = value;
}
else
{
_headers.Add(key, value);
}
return (T)this;
}

Expand All @@ -79,7 +86,16 @@ public T WithHeader(string key, string value)
public T WithHeaders(IDictionary<string, string> headers)
{
foreach (var itm in headers)
_headers.Add(itm.Key, itm.Value);
{
if( _headers.ContainsKey(itm.Key))
{
_headers[itm.Key] = itm.Value;
}
else
{
_headers.Add(itm.Key, itm.Value);
}
}
return (T)this;
}

Expand Down
4 changes: 3 additions & 1 deletion src/GeneralTools/DataverseClient/Client/ConnectionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ internal System.Net.NetworkCredential DataverseServiceAccessCredential
/// <summary>
/// Type of protocol to use
/// </summary>
internal string InternetProtocalToUse { get { return _InternetProtocalToUse; } set { _InternetProtocalToUse = value; } }
internal string InternetProtocolToUse { get { return _InternetProtocalToUse; } set { _InternetProtocalToUse = value; } }

/// <summary>
/// returns the connected organization detail object.
Expand Down Expand Up @@ -2337,6 +2337,8 @@ internal async Task<HttpResponseMessage> Command_WebExecuteAsync(string queryStr
string requestIdLogSegement = logEntry.GetFormatedRequestSessionIdString(requestTrackingId, SessionTrackingId);
do
{
retry = false; // Set intial state.

// Add authorization header. - Here to catch the situation where a token expires during retry.
if (!customHeaders.ContainsKey(Utilities.RequestHeaders.AUTHORIZATION_HEADER))
customHeaders.Add(Utilities.RequestHeaders.AUTHORIZATION_HEADER, new List<string>() { string.Format("Bearer {0}", await RefreshClientTokenAsync().ConfigureAwait(false)) });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
using System.IdentityModel.Tokens;
using System.Linq;
using System.Security;
using System.Security.Permissions;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceModel.Security;
using System.Text;

using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Common;
Expand Down Expand Up @@ -52,6 +52,7 @@ internal ServiceConfiguration(Uri serviceUri, bool checkForSecondary)

ClientExceptionHelper.ThrowIfNull(ServiceEndpointMetadata, "ServiceEndpointMetadata");

#if NETFRAMEWORK
if (ServiceEndpointMetadata.ServiceEndpoints.Count == 0)
{
StringBuilder errorBuilder = new StringBuilder();
Expand All @@ -65,6 +66,7 @@ internal ServiceConfiguration(Uri serviceUri, bool checkForSecondary)

throw new InvalidOperationException(ClientExceptionHelper.FormatMessage(0, "The provided uri did not return any Service Endpoints!\n{0}", errorBuilder.ToString()));
}
#endif

ServiceEndpoints = ServiceEndpointMetadata.ServiceEndpoints;

Expand Down Expand Up @@ -665,20 +667,20 @@ private SecurityTokenResponse AuthenticateInternal(AuthenticationCredentials aut
{
return Issue(authenticationCredentials);
}
catch (SecurityTokenValidationException)
{
retry = false;

// Fall back to windows integrated.
if (authenticationCredentials.IssuerEndpoints.ContainsKey(TokenServiceCredentialType.Windows.ToString()))
{
authenticationCredentials.EndpointType = TokenServiceCredentialType.Windows;
retry = ++retryCount < 2;
}

// We don't care, we just want to return null. The reason why we are are catching this one is because in pure Kerberos mode, this
// will throw a very bad exception that will crash VS.
}
//catch (SecurityTokenValidationException) // Removed due to a type conflict with DV Server
//{
// retry = false;

// // Fall back to windows integrated.
// if (authenticationCredentials.IssuerEndpoints.ContainsKey(TokenServiceCredentialType.Windows.ToString()))
// {
// authenticationCredentials.EndpointType = TokenServiceCredentialType.Windows;
// retry = ++retryCount < 2;
// }

// // We don't care, we just want to return null. The reason why we are are catching this one is because in pure Kerberos mode, this
// // will throw a very bad exception that will crash VS.
//}
catch (SecurityNegotiationException)
{
// This is the exception with Integrated Windows Auth.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
using System.Text;
using System.Xml;
using Microsoft.PowerPlatform.Dataverse.Client.Utils;
//using Microsoft.Crm.Protocols.WSTrust.Bindings;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Common;
Expand Down Expand Up @@ -262,13 +261,13 @@ public static IssuerEndpoint GetIssuer(Binding binding)
}

return null;
}
}

#if NETFRAMEWORK
private static KerberosSecurityTokenParameters GetKerberosTokenParameters(SecurityBindingElement securityElement)
{
if (securityElement != null)
{
#if NETFRAMEWORK
if (securityElement.EndpointSupportingTokenParameters != null)
{
if (securityElement.EndpointSupportingTokenParameters.Endorsing != null)
Expand All @@ -279,15 +278,12 @@ private static KerberosSecurityTokenParameters GetKerberosTokenParameters(Securi
}
}
}
#else
throw new PlatformNotSupportedException("Xrm.Sdk WSTrust");
#endif
}

return null;
}
#endif

private static IssuedSecurityTokenParameters GetIssuedTokenParameters(SecurityBindingElement securityElement)
private static IssuedSecurityTokenParameters GetIssuedTokenParameters(SecurityBindingElement securityElement)
{
if (securityElement != null)
{
Expand Down Expand Up @@ -351,29 +347,27 @@ public static CustomBinding SetIssuer(Binding binding, IssuerEndpoint issuerEndp
}

return new CustomBinding(elements);
}
}

#if NETFRAMEWORK
private static void ParseEndpoints(ServiceEndpointDictionary serviceEndpoints, ServiceEndpointCollection serviceEndpointCollection)
{
serviceEndpoints.Clear();

if (serviceEndpointCollection != null)
{
#if NETFRAMEWORK
foreach (var endpoint in serviceEndpointCollection)
{
if (IsEndpointSupported(endpoint))
{
serviceEndpoints.Add(endpoint.Name, endpoint);
}
}
#else
throw new PlatformNotSupportedException("Xrm.Sdk WSDL");
#endif
}
}
#endif

private static bool IsEndpointSupported(ServiceEndpoint endpoint)
private static bool IsEndpointSupported(ServiceEndpoint endpoint)
{
if (endpoint != null)
{
Expand All @@ -389,6 +383,7 @@ private static bool IsEndpointSupported(ServiceEndpoint endpoint)

internal static ServiceEndpointMetadata RetrieveServiceEndpointMetadata(Type contractType, Uri serviceUri, bool checkForSecondary)
{
#if NETFRAMEWORK // WebInfra; MetadataSet and CreateMetadataClient are NETFRAMEWORK-ONLY
ServiceEndpointMetadata serviceEndpointMetadata = new ServiceEndpointMetadata();

serviceEndpointMetadata.ServiceUrls = ServiceConfiguration<IOrganizationService>.CalculateEndpoints(serviceUri);
Expand All @@ -398,25 +393,17 @@ internal static ServiceEndpointMetadata RetrieveServiceEndpointMetadata(Type con
serviceEndpointMetadata.ServiceUrls.AlternateEndpoint = null;
}

#if !NETFRAMEWORK
// TODO: Waiting on work for updated WCF endpoints collection to be completed. // hard throw here to prevent any futher progress.
throw new PlatformNotSupportedException("Xrm.Sdk WSDL");
#endif

// Get version of current assembly which is the version of the SDK
#pragma warning disable CS0162 // Unreachable code detected
Version sdkVersion = GetSDKVersionNumberFromAssembly();
#pragma warning restore CS0162 // Unreachable code detected
var wsdlUri = new Uri(string.Format(CultureInfo.InvariantCulture, "{0}{1}&sdkversion={2}", serviceUri.AbsoluteUri, "?wsdl", sdkVersion.ToString(2)));

var mcli = CreateMetadataClient(wsdlUri.Scheme);
if (mcli != null)
{
#if NETFRAMEWORK
try
{
serviceEndpointMetadata.ServiceMetadata = mcli.GetMetadata(wsdlUri, MetadataExchangeClientMode.HttpGet);
}
}
catch (InvalidOperationException ioexp)
{
bool rethrow = true;
Expand Down Expand Up @@ -447,29 +434,14 @@ internal static ServiceEndpointMetadata RetrieveServiceEndpointMetadata(Type con
throw;
}
}
#else
throw new PlatformNotSupportedException("Xrm.Sdk WSDL");
#endif
}
else
{
#if !NETFRAMEWORK

if (serviceEndpointMetadata.ServiceMetadata == null)
serviceEndpointMetadata.ServiceMetadata = new MetadataSet();
var MetadataBody = GetMexDocument(wsdlUri);
#else
throw new PlatformNotSupportedException("Xrm.Sdk WSDL");
#endif
}

ClientExceptionHelper.ThrowIfNull(serviceEndpointMetadata.ServiceMetadata, "STS Metadata");
ClientExceptionHelper.ThrowIfNull(serviceEndpointMetadata.ServiceMetadata, "STS Metadata");

var contracts = CreateContractCollection(contractType);

if (contracts != null)
{
#if NETFRAMEWORK
// The following code inserts a custom WsdlImporter without removing the other
// importers already in the collection.
var importer = new WsdlImporter(serviceEndpointMetadata.ServiceMetadata);
Expand Down Expand Up @@ -497,34 +469,15 @@ internal static ServiceEndpointMetadata RetrieveServiceEndpointMetadata(Type con
}

ParseEndpoints(serviceEndpointMetadata.ServiceEndpoints, endpoints);
#else

// Dataverse requires Message Transport security which is not supported in .net core for ActiveDirectory.


//AuthenticationPolicy authenticationPolicy = new AuthenticationPolicy();
//authenticationPolicy.PolicyElements.Add("AuthenticationType", "ActiveDirectory"); // Need to read these from metdata in the future if WCF does not provide support/.
//TextMessageEncodingBindingElement text01 = new TextMessageEncodingBindingElement();
//HttpsTransportBindingElement http1 = new HttpsTransportBindingElement();
//http1.ExtendedProtectionPolicy = new System.Security.Authentication.ExtendedProtection.ExtendedProtectionPolicy(System.Security.Authentication.ExtendedProtection.PolicyEnforcement.WhenSupported, System.Security.Authentication.ExtendedProtection.ProtectionScenario.TransportSelected, null);
//CustomBinding bind = new CustomBinding(authenticationPolicy, new TextMessageEncodingBindingElement(), http1);
//bind.Name = "CustomBinding_IOrganizationService";
//bind.Namespace = "http://schemas.microsoft.com/xrm/2011/Contracts/Services";
//serviceEndpointMetadata.ServiceEndpoints.Add(
// "CustomBinding_IOrganizationService",
// new ServiceEndpoint(contracts[0],
// bind,
// new EndpointAddress(serviceEndpointMetadata.ServiceUrls.PrimaryEndpoint)));


throw new PlatformNotSupportedException("Xrm.Sdk WSDL");
#endif
}

return serviceEndpointMetadata;
#else
throw new NotImplementedException("ServiceModel metadata support is limited for this target framework");
#endif
}

private static Version GetSDKVersionNumberFromAssembly()
private static Version GetSDKVersionNumberFromAssembly()
{
string fileVersion = OrganizationServiceProxy.GetXrmSdkAssemblyFileVersion();

Expand All @@ -536,8 +489,9 @@ private static Version GetSDKVersionNumberFromAssembly()
}

return parsedVersion;
}
}

#if NETFRAMEWORK
/// <summary>
/// Returns a list of policy import extensions in the importer parameter and adds a SecurityBindingElementImporter if not already present in the list.
/// </summary>
Expand All @@ -546,7 +500,7 @@ private static Version GetSDKVersionNumberFromAssembly()
private static List<IPolicyImportExtension> AddSecurityBindingToPolicyImporter(WsdlImporter importer)
{
List<IPolicyImportExtension> newExts = new List<IPolicyImportExtension>();
#if NETFRAMEWORK


KeyedByTypeCollection<IPolicyImportExtension> policyExtensions = importer.PolicyImportExtensions;
SecurityBindingElementImporter securityBindingElementImporter = policyExtensions.Find<SecurityBindingElementImporter>();
Expand All @@ -564,18 +518,14 @@ private static List<IPolicyImportExtension> AddSecurityBindingToPolicyImporter(W
newExts.AddRange(policyExtensions);

return newExts;
#else

newExts.Add(new AuthenticationPolicyImporter(new SecurityBindingElementImporter()));
return newExts;
//throw new PlatformNotSupportedException("Xrm.Sdk WSDL");
#endif
}
#endif

[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Need to catch any exception here and fail.")]
#if NETFRAMEWORK
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Need to catch any exception here and fail.")]
private static bool TryRetrieveMetadata(MetadataExchangeClient mcli, Uri serviceEndpoint, ServiceEndpointMetadata serviceEndpointMetadata)
{
#if NETFRAMEWORK

bool rethrow = true;
try
{
Expand All @@ -589,24 +539,22 @@ private static bool TryRetrieveMetadata(MetadataExchangeClient mcli, Uri service
}

return rethrow;
#else
throw new PlatformNotSupportedException("Xrm.Sdk WSDL");
#endif
}
#endif

private static XmlQualifiedName GetPortTypeQName(ContractDescription contract)
private static XmlQualifiedName GetPortTypeQName(ContractDescription contract)
{
return new XmlQualifiedName(contract.Name, contract.Namespace);
}

private static Collection<ContractDescription> CreateContractCollection(Type contract)
{
return new Collection<ContractDescription> { ContractDescription.GetContract(contract) };
}
}

#if NETFRAMEWORK
private static MetadataExchangeClient CreateMetadataClient(string scheme)
{
#if NETFRAMEWORK
WSHttpBinding mexBinding = null;

if (string.Compare(scheme, "https", StringComparison.OrdinalIgnoreCase) == 0)
Expand All @@ -628,13 +576,10 @@ private static MetadataExchangeClient CreateMetadataClient(string scheme)
mcli.MaximumResolvedReferences = 100;

return mcli;
#else
return null;
//throw new PlatformNotSupportedException("Xrm.Sdk WSDL");
#endif
}
#endif

public static void ReplaceEndpointAddress(ServiceEndpoint endpoint, Uri adddress)
public static void ReplaceEndpointAddress(ServiceEndpoint endpoint, Uri adddress)
{
var addressBuilder = new EndpointAddressBuilder(endpoint.Address);
addressBuilder.Uri = adddress;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -369,8 +369,8 @@ public object BeforeSendRequest(ref Message request, IClientChannel channel)
{
using (OperationContextScope scope = new OperationContextScope((IContextChannel)channel))
{
var ForceConsistencytHeader = new MessageHeader<string>("Strong").GetUntypedHeader(Utilities.RequestHeaders.FORCE_CONSISTENCY, "http://schemas.microsoft.com/xrm/2011/Contracts");
request.Headers.Add(ForceConsistencytHeader);
var ForceConsistencyHeader = new MessageHeader<string>("Strong").GetUntypedHeader(Utilities.RequestHeaders.FORCE_CONSISTENCY, "http://schemas.microsoft.com/xrm/2011/Contracts");
request.Headers.Add(ForceConsistencyHeader);
}
}
return null;
Expand Down
Loading

0 comments on commit be9ee13

Please sign in to comment.