public static EndpointDescription SelectEndpointBySecurity(string discoveryUrl, SecurityPolicy securityPolicy, MessageSecurity messageSecurity, int operationTimeout = -1) { Uri uri = new Uri(discoveryUrl); EndpointConfiguration configuration = EndpointConfiguration.Create(); if (operationTimeout > 0) { configuration.OperationTimeout = operationTimeout; } EndpointDescription selectedEndpoint = null; MessageSecurityMode actualMessageSecurity; switch (messageSecurity) { case MessageSecurity.SignAndEncrypt: actualMessageSecurity = MessageSecurityMode.SignAndEncrypt; break; case MessageSecurity.Sign: actualMessageSecurity = MessageSecurityMode.Sign; break; case MessageSecurity.None: default: actualMessageSecurity = MessageSecurityMode.None; break; } // Connect to the server's discovery endpoint and find the available configuration. using (DiscoveryClient client = DiscoveryClient.Create(uri, configuration)) { EndpointDescriptionCollection endpoints = client.GetEndpoints(null); selectedEndpoint = endpoints.FirstOrDefault(ep => ep.SecurityMode == actualMessageSecurity && ep.SecurityPolicyUri.Contains(securityPolicy.ToString())); } return(selectedEndpoint); }
/// <summary> /// Select the best supported endpoint from an /// EndpointDescriptionCollection, with or without security. /// </summary> /// <param name="url"></param> /// <param name="endpoints"></param> /// <param name="useSecurity"></param> public static EndpointDescription SelectEndpoint( Uri url, EndpointDescriptionCollection endpoints, bool useSecurity) { EndpointDescription selectedEndpoint = null; // select the best endpoint to use based on the selected URL and the UseSecurity checkbox. for (int ii = 0; ii < endpoints.Count; ii++) { EndpointDescription endpoint = endpoints[ii]; // check for a match on the URL scheme. if (endpoint.EndpointUrl.StartsWith(url.Scheme)) { // check if security was requested. if (useSecurity) { if (endpoint.SecurityMode == MessageSecurityMode.None) { continue; } // skip unsupported security policies if (SecurityPolicies.GetDisplayName(endpoint.SecurityPolicyUri) == null) { continue; } } else { if (endpoint.SecurityMode != MessageSecurityMode.None) { continue; } } // pick the first available endpoint by default. if (selectedEndpoint == null) { selectedEndpoint = endpoint; } // The security level is a relative measure assigned by the server to the // endpoints that it returns. Clients should always pick the highest level // unless they have a reason not too. // Some servers however, mess this up a bit. So prefer a higher SecurityMode // over the SecurityLevel. if (endpoint.SecurityMode > selectedEndpoint.SecurityMode || (endpoint.SecurityMode == selectedEndpoint.SecurityMode && endpoint.SecurityLevel > selectedEndpoint.SecurityLevel)) { selectedEndpoint = endpoint; } } } // pick the first available endpoint by default. if (selectedEndpoint == null && endpoints.Count > 0) { selectedEndpoint = endpoints.FirstOrDefault(e => e.EndpointUrl?.StartsWith(url.Scheme) == true); } // return the selected endpoint. return(selectedEndpoint); }