title: How can I pass a Soap Header to a SOAP WCF Service in Visual Studio?
body: <p>I'm trying to consume a third-party web service (discription on russian language) <a href="https://92.46.122.150:8443/esf-web/ws/SessionService?wsdl" rel="nofollow">https://92.46.122.150:8443/esf-web/ws/SessionService?wsdl</a></p>
  
  <p>I am trying to connect to a website using method "createSession", get Id session and close session in a website.
  Do this I using Visual Studio 2013, C#, .NET 4.5, WSE 3.0 (Microsoft.Web.Services3.dll).</p>
  
  <p>I have already created project in Visual Studio 2013 "Windows Forms Application". In this form user can enter user name, login and to choose his a digital signature certificate. Also I added web service as a "web service reference" but I'm not sure how to pass the credentials for the header.</p>
  
  <p>I'm trying to do this:</p>
  
  <pre><code>string strPasswordCertificate = "123456";
  string strCertificate = "C:/Test/AUTH_RSA_db79bb07b4722c042e025979b3b11995fc46765b.p12";
  X509Certificate x509_CertAUTH = new X509Certificate(strCertificateFilePathAUTH, strPasswordCertificate);
  string strCertAUTH = x509_CertAUTH.ToString();
  
  CreateSessionRequest CreateReq = new CreateSessionRequest();
  
  CreateReq.x509Certificate = strCertAUTH;
  string strIIN = "753159846249";
  
  CreateReq.tin = strIIN;
  WS.createSession(CreateReq);
  </code></pre>
  
  <p>But when run a programm Visual studio show an error on a line with "WS.createSession(CreateReq)" like below:</p>
  
  <p><em>SoapHeaderException was unhandled.
  An unhandled exception of type "System.Web.Service.Protocols.SoapHeaderExeption" occurred in System.Web.Services.dll
  Additional information: An error was discovered processing the "wsse:Security" header.</em></p>
  
  <p>This exception is thrown when an XML Web service method is called over SOAP and an exception occurs during processing of the SOAP header.
  After that I made changes in my code like below:</p>
  
  <pre><code>string strPasswordCertificate = "123456";
  string strCertificate = "C:/Test/AUTH_RSA_db79bb07b4722c042e025979b3b11995fc46765b.p12";
  X509Certificate x509_CertAUTH = new X509Certificate(strCertificateFilePathAUTH, strPasswordCertificate);
  string strCertAUTH = x509_CertAUTH.ToString();
  
  CreateSessionRequest CreateReq = new CreateSessionRequest();
  
  CreateReq.x509Certificate = strCertAUTH;
  string strIIN = "753159846249";
  
  CreateReq.tin = strIIN;
  WS.createSession(CreateReq);
  
  string _userName = "123456789011";
  string _UserPassword = "TestPass123";
  
  UsernameToken userToken;
  
  userToken = new UsernameToken(_userName, _UserPassword, PasswordOption.SendPlainText);
  
  SessionService WS = new SessionService();
  SoapContext requestContext = WS.RequestSoapContext;
  requestContext.Security.Tokens.Add(userToken);
  
  WS.createSession(CreateReq);
  </code></pre>
  
  <p>But when run a programm Visual studio show error on a line with "WS.createSession(CreateReq)" like below:</p>
  
  <p><em>SoapHeaderException was unhandled.
  An unhandled exception of type 'System.Web.Service.Protocols.SoapHeaderExeption' occurred in System.Web.Services.dll
  Additional information: An invalid security token was provided.</em></p>
  
  <p>What I need more to do or change in my code that web reverence start to work? Any idea?</p>