How can I pass a username/password in the header to a SOAP WCF Service

后端 未结 9 1785
长发绾君心
长发绾君心 2020-11-27 13:03

I\'m trying to consume a third-party web service https://staging.identitymanagement.lexisnexis.com/identity-proofing/services/identityProofingServiceWS/v2?wsdl

I alr

9条回答
  •  北荒
    北荒 (楼主)
    2020-11-27 13:22

    Obviously it has been some years this post has been alive - but the fact is I did find it when looking for a similar issue. In our case, we had to add the username / password info to the Security header. This is different from adding header info outside of the Security headers.

    The correct way to do this (for custom bindings / authenticationMode="CertificateOverTransport") (as on the .Net framework version 4.6.1), is to add the Client Credentials as usual :

        client.ClientCredentials.UserName.UserName = "[username]";
        client.ClientCredentials.UserName.Password = "[password]";
    

    and then add a "token" in the security binding element - as the username / pwd credentials would not be included by default when the authentication mode is set to certificate.

    You can set this token like so:

        //Get the current binding 
        System.ServiceModel.Channels.Binding binding = client.Endpoint.Binding;
        //Get the binding elements 
        BindingElementCollection elements = binding.CreateBindingElements();
        //Locate the Security binding element
        SecurityBindingElement security = elements.Find();
    
        //This should not be null - as we are using Certificate authentication anyway
        if (security != null)
        {
        UserNameSecurityTokenParameters uTokenParams = new UserNameSecurityTokenParameters();
        uTokenParams.InclusionMode = SecurityTokenInclusionMode.AlwaysToRecipient;
    security.EndpointSupportingTokenParameters.SignedEncrypted.Add(uTokenParams);
        }
    
       client.Endpoint.Binding = new CustomBinding(elements.ToArray());
    

    That should do it. Without the above code (to explicitly add the username token), even setting the username info in the client credentials may not result in those credentials passed to the Service.

提交回复
热议问题