Tuesday, April 1, 2014

How to add user name token to the soap header :VS2012

When calling secure old asmx web service using  c# we cant directly add user name token to the soap header like WCF.we need to have Microsoft.Web.Services(Version) library.Microsoft has not shipped this library along with the .Net framework.Instead of that Microsoft has provided package called Web Services Enhancements which is a product that enables you to build secure Web services quickly and easily.

There are few versions for WSE. Downloaded and install  latest version from below link(3.0).


after installed go to the below location.(my DEV machine is win7 64 bit version- path may change according to the your windows version)


Then add Microsoft.Web.Service3.dll reference to your project.


Then add the web service reference to the project and open reference.cs file.(to open that click show all files)


Open proxy file.


you can see our generated soap class inherited from SoapHttpClientProtocol. we need to change it to the Microsoft.Web.Services3.WebServicesClientProtocol .


Now we are done with the proxy class. then need to add the blow code to the web service client.

   1: TestHost.ManufacturingSOAP soupClinet = new TestHost.ManufacturingSOAP();
   2: TestHost.deleteIssueRequest rptIssue = new TestHost.deleteIssueRequest();
   4: // create user nam token
   5: UsernameToken userToken = new UsernameToken("username", "password", PasswordOption.SendPlainText);
   7: // get reference to the proxy request context.
   8: SoapContext requestContext = soupClinet.RequestSoapContext;
  10: // then add user token to the security context.
  11: requestContext.Security.Tokens.Add(userToken);
  13: try
  14: {
  15:     var result = soupClinet.deleteIssue(rptIssue);              
  16: }
  17: catch (Exception ex)
  18: {
  19:     Console.WriteLine(ex.Message);
  20: }
  22: Console.Read();

No comments:

Post a Comment