Search code examples
asp.netweb-serviceswcfwsdlsaml

Using SAML token with Web Service (wsdl)


I have been given a .wsdl file and .pfx from the provider.

I call the IdP and acquire a SAML token. Now I need to pass that token to the WebService.

How do I use the SAML token to work with the WebService?

I am using .NET 4.5


Solution

  • I was able to add the token and get response with the help of the following two posts:

    http://www.noiseworks.org/security-token-service-in-asp-net-application-part-2/ http://travisspencer.com/blog/2012/01/cryptographic-operations-are-r.html

    Here's my code:

      private static string serviceEndpoint = "https service endpoint";
        public static void CallProviderService(SecurityToken token)
        {
            var binding = new WS2007FederationHttpBinding(WSFederationHttpSecurityMode.TransportWithMessageCredential);
            binding.Security.Message.EstablishSecurityContext = false;
            binding.Security.Message.IssuedKeyType = SecurityKeyType.BearerKey;
    
            var channelFactory = new ChannelFactory<ISomeProviderService>(binding, new EndpointAddress(new Uri(serviceEndpoint)));
            string thumb = "mycertthumbprint";
            channelFactory.Credentials.ClientCertificate.SetCertificate(StoreLocation.CurrentUser, StoreName.My, X509FindType.FindByThumbprint, thumb);
            channelFactory.Credentials.ServiceCertificate.Authentication.CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.PeerOrChainTrust;
            channelFactory.ConfigureChannelFactory();
            channelFactory.Credentials.SupportInteractive = false;
    
    var elements = service.Endpoint.Binding.CreateBindingElements();
    elements.Find<SecurityBindingElement>().EnableUnsecuredResponse = true;
    service.Endpoint.Binding = new CustomBinding(elements);
    
            var channel = channelFactory.CreateChannelWithIssuedToken<ISomeProviderService>(token);
    
            try
            {
                var response = channel.MyServiceMethod(somedataobject);
            }
    
            catch (Exception ex)
            {
               //log message
            }
        }