0

I'm trying to add a SOAP header with authentication information such as username and password similar to this question posted: Adding Soap implicit headers to WSDL.

The proxy class is auto-generated using VS WSDL utility when I click add web reference but it doesn't auto generate any authentication class.

The answer that this user provided seems reasonable, however I am stuck on where to place the public partial class Security : SoapHeader. Any help would be appreciated.

The users answer is here:

You can add soap header information to method calls by decorating the methods in the proxy class generated from the wsdl with the SoapHeader attribute.

For example wsdl.exe will generate client proxy class Reference.cs for the web service reference when you "Add Web Reference". In the link mentioned above https://stage.totalcheck.sensis.com.au/service/webservice?wsdl there is a message suggestAddress which will translate to a method in the generated reference.cs client proxy code file when you add a web reference from visual studio. By default when this method is called there will be no Header in the soap envelope. To add a SoapHeader to the envelope for this request add a [SoapHeader("Security")] attribute to the top of the SuggestAddress method in the Reference.cs generated class, where "Security" is a class that inherits from SoapHeader base class.

Example for the above required Security SoapHeader you would create the following classes,

public partial class Security : SoapHeader
{
    public UserNameToken UserNameToken { get; set; }
}

public partial class UserNameToken
{
    public string UserName { get; set; }
    public string Password { get; set; }
}

Then you would decorate the SuggestAddress method in the reference.cs like followed,

[SoapHeader("Security")]
public suggestAddressesResult suggestAddresses([System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] addressSearch search) {
        object[] results = this.Invoke("suggestAddresses", new object[] {search});
        return ((suggestAddressesResult)(results[0]));
    }

This will ensure that every envelope created when method suggestAddress is invoked contains a security header that looks like the one mentioned above,

<soapenv:Header>
    <wsse:Security>
        <wsse:UsernameToken>
            <wsse:Username>username</wsse:Username>
            <wsse:Password>password</wsse:Password>
        </wsse:UsernameToken>
    </wsse:Security>
Community
  • 1
  • 1
csharpvsto
  • 89
  • 1
  • 8
  • Did you know that the ASMX technology you're using is a legacy technology, and should not be used for new development? WCF or ASP.NET Web API should be used for all new development of web service clients and servers. One hint: Microsoft has retired the [ASMX Forum](http://social.msdn.microsoft.com/Forums/en-US/asmxandxml/threads) on MSDN. – John Saunders Jan 14 '14 at 05:41
  • Thanks, I've realised that the wsdl utility doesn't support importing of wsp policy. So I've used the add service reference method instead which uses the svcutility. I've successfully added username and password to the header using myservice.clientcredentials.username.username = "myusername" and the same for password. Do you have any idea on how to add the nonce and timestamp variables? – csharpvsto Jan 14 '14 at 23:22
  • 1
    No, sorry, maybe other readers will know. I'm changing the tag to [tag:wcf] to attract their attention. – John Saunders Jan 15 '14 at 00:18
  • It seems the service you're calling is using WS-Security and possibly other WS-* standards. Unless there service is using some kind of custom authentication scheme over WS-Security, the generated WCF proxy can be configured to support standard authentication options (HTTP Basic, Forms, NTLM, Kerberos, etc.) out-of-the-box without needing to add any soap authentication headers. – Sixto Saez Jan 15 '14 at 02:36
  • How can I configure the WCF proxy to do that? FOr each SOAP message I send, it must contain the authentication information in the headers. I've looked at this: http://stackoverflow.com/a/964461/3163980 and it seems good except that my header needs to look like this for time stamps: 2014-01-05T22:25:10.334Z So is there a way to do this using this method? i.e. add child created node to the timestamp header & wsu:Id="Timestamp-2" id – csharpvsto Jan 15 '14 at 02:59

0 Answers0