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

后端 未结 2 1396
盖世英雄少女心
盖世英雄少女心 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: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

提交回复
热议问题