Question

I'm trying to add a WSSE SOAP Header to my service call, but most of the examples focusses on WCF. I'm not making using of WCF. I have added a Web Reference (WSDL).

I have tried various methods without success, like - overriding the GetWebRequest method:

    protected override System.Net.WebRequest GetWebRequest(Uri uri)
    {
        string user = "username";
        string pwd = "password";

        System.Net.WebRequest request = base.GetWebRequest(uri);

        string auth = string.Format("Basic {0}", Convert.ToBase64String(System.Text.Encoding.Default.GetBytes(string.Format("{0}:{1}", user, pwd))));
        request.PreAuthenticate = true;
        request.AuthenticationLevel = System.Net.Security.AuthenticationLevel.MutualAuthRequested;
        request.Headers.Add(HttpRequestHeader.Authorization, auth);

        return request;
    }

The WSSE Security Header should resemble something like this:

<Security xmlns="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
         <UsernameToken>
            <Username>username</Username>
            <Password>password</Password>
         </UsernameToken>
      </Security>

Many thanks in advance! Kind regards,

OTHER TIPS

Tried adding a comment but it was too long...

The second link is still OK? Anyway, instead of a link, here is the key information I think (in case the link does disappear) - replace the reference to System.Web.Services.Protocols.SoapHttpClientProtocol with Microsoft.Web.Services2.WebServicesClientProtocol (or Microsoft.Web.Services3.WebServicesClientProtocol, if that works for you - I found services2 via nuget). We need to change the class because the request method in SoapHttpClientProtocol is protected, making it difficult (impossible?) to adjust any of the request details. I assume that changing the class opens up all of the possibilities afforded to you if you had used WCF to generate your code. If you are adding username then you will probably want add all of the other SOAP security values, so Correct way communicate WSSE Usernametoken for SOAP webservice is a handy reference.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top