Using SAML token with Web Service (wsdl)

2019-03-04 02:34发布

问题:

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

回答1:

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
        }
    }


回答2:

This is something that should be specified by the provider of the WS. A common standard i the WS-security standard by OASIS

Using this standard the SAML Assertion is placed in a SOAP security header