Use wsse security header in soap message (Visual Studio 2015, .Net Framework 4.5)

后端 未结 2 1390
盖世英雄少女心
盖世英雄少女心 2021-01-14 11:51

I would like to consume a Soap Service provided by DHL. You can find the wsdl here: https://wsbexpress.dhl.com/sndpt/expressRateBook?WSDL

Therefore I created a new C

相关标签:
2条回答
  • 2021-01-14 12:03

    You can configure you Service Reference to add the Security Header as AW Rowse describes at http://cxdeveloper.com/article/implementing-ws-security-digest-password-nonce-net-40-wcf:

     private void Configure()
        {
            System.Net.ServicePointManager.ServerCertificateValidationCallback = (senderX, certificate, chain, sslPolicyErrors) => { return true; };
    
            defaultBinding = new BasicHttpBinding
            {
                Security =
                {
                    Mode = BasicHttpSecurityMode.Transport,
                    Transport =
                    {
                        ClientCredentialType = HttpClientCredentialType.Digest
                    }
                }
            };
    
            defaultToken = new UsernameToken(UserName, Password, PasswordOption.SendHashed);
    
            defaultSecurityHeader = MessageHeader.CreateHeader(
              "Security",
              "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd",
              defaultToken.GetXml(new XmlDocument())
            );
        }
    

    And create you client/proxy like this:

    public consulta_informacao_respttClient CriaConsultaClinicaClient()
        {
            var client = new consulta_informacao_respttClient(defaultBinding, new EndpointAddress("https://resqa.homologacao.unimed.coop.br/chs-integration-external-services-ptu-clinical/proxy-services/execute-query/execute-query-proxy-service"));
            client.ClientCredentials.UserName.UserName = UserName;
            client.ClientCredentials.UserName.Password = Password;
    
            var scope = new OperationContextScope(client.InnerChannel);
    
            OperationContext.Current.OutgoingMessageHeaders.Add(defaultSecurityHeader);
            return client;
        }
    

    The properties you will need to create in your class are:

    private BasicHttpBinding defaultBinding;
    private UsernameToken defaultToken;
    private MessageHeader defaultSecurityHeader;
    

    You won't need to configure anything in app/web.config.

    0 讨论(0)
  • 2021-01-14 12:17


    You can add the Web Reference in compatibility mode (I am guessing you are doing so). If you are not adding the reference in compatibility mode, do the following:

    Right click on references Add Service Reference-> Advanced -> Add Web Reference (Below the compatibility section), type the URL of the WS and add the reference.

    The WSE2.0 extensions are available as a Nuget Package at:

    https://www.nuget.org/packages/Microsoft.Web.Services2/

    Install the nuget package on the package manager console running the following nugget command: Install-Package Microsoft.Web.Services2

    After you installed the nuget package, you need to make sure your project is referencing the following DLL's:

    1. System.Web
    2. System.Web.Services
    3. Microsoft.Web.Services2 (This will be added after you install the nuget package)

    In order to use the WSE2.0 extensions, you need to actually modify the Proxy class that was created when you added the WebReference to inherit from "Microsoft.Web.Services2.WebServicesClientProtocol" instead of "System.Web.Services.Protocols.SoapHttpClientProtocol". Be aware that if you update the WebReference, the Proxy class will inherit againfrom SoapHttpClientProtocol.

    Add the following using clauses to the code consuming the Proxy class:

    using Microsoft.Web.Services2;
    using Microsoft.Web.Services2.Security;
    using Microsoft.Web.Services2.Security.Tokens;
    

    After you make this changes, you code should look something like this:

    var token = new UsernameToken("theUser", "thePassword", PasswordOption.SendHashed);
    
    var serviceProxy = new ExpressRateBook.gblExpressRateBook();
    SoapContext requestContext = serviceProxy.RequestSoapContext;
    requestContext.Security.Timestamp.TtlInSeconds = 60;
    requestContext.Security.Tokens.Add(token);
    //The rest of the logic goes here...
    

    I added the screenshot down below for your reference:

    NOTE: I was unable to test the code since I am unfamiliar with the actual methods that you need to consume, the code displayed is just an example of what I saw in the proxy class, update it according to your needs. It should work fine if you follow the steps described before. Check the following link for more detailed instructions:

    https://msdn.microsoft.com/en-us/library/ms819938.aspx

    0 讨论(0)
提交回复
热议问题