title: Authentication of external requests - how to pass user credentials in the SOAP header?
body: <p>There is a simple WCF Web service. All external requests to the web service must be authenticated.</p>
  
  <p>The contract of the Web- service:</p>
  
  <pre><code>namespace SimpleCustomService
  {
      [ServiceContract]
      public interface UsrIService
      {
          [OperationContract]
          string SayHello();
      }
  }
  </code></pre>
  
  <p>The implementation of the Web- service:</p>
  
  <pre><code>namespace SimpleCustomService
  {
      public class UsrService : UsrIService
      {
          public string SayHello()
          {
              return "Hello";
          }
      }
  }
  </code></pre>
  
  <p>By using the <code>wsdl.exe</code> utility I generate proxy class to invoke methods of the Web service.</p>
  
  <p>The generated code looks as follows:</p>
  
  <pre><code>using System;
  using System.ComponentModel;
  using System.Diagnostics;
  using System.Web.Services;
  using System.Web.Services.Protocols;
  using System.Xml.Serialization;
  
  // 
  // This source code was auto-generated by wsdl, Version=4.6.1055.0.
  // 
  
  
  /// &lt;remarks/&gt;
  [System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "4.6.1055.0")]
  [System.Diagnostics.DebuggerStepThroughAttribute()]
  [System.ComponentModel.DesignerCategoryAttribute("code")]
  [System.Web.Services.WebServiceBindingAttribute(Name="BasicHttpBinding_UsrIService", Namespace="http://tempuri.org/")]
  public partial class UsrService : System.Web.Services.Protocols.SoapHttpClientProtocol {
  
      private System.Threading.SendOrPostCallback SayHelloOperationCompleted;
  
      /// &lt;remarks/&gt;
      public UsrService() {
          this.Url = "http://localhost:8080/0/ServiceModel/SimpleCustomService.svc/soap";
      }
  
      /// &lt;remarks/&gt;
      public event SayHelloCompletedEventHandler SayHelloCompleted;
  
      /// &lt;remarks/&gt;
      [System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://tempuri.org/UsrIService/SayHello", RequestNamespace="http://tempuri.org/", ResponseNamespace="http://tempuri.org/", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
      [return: System.Xml.Serialization.XmlElementAttribute(IsNullable=true)]
      public string SayHello() {
          object[] results = this.Invoke("SayHello", new object[0]);
          return ((string)(results[0]));
      }
  
      /// &lt;remarks/&gt;
      public System.IAsyncResult BeginSayHello(System.AsyncCallback callback, object asyncState) {
          return this.BeginInvoke("SayHello", new object[0], callback, asyncState);
      }
  
      /// &lt;remarks/&gt;
      public string EndSayHello(System.IAsyncResult asyncResult) {
          object[] results = this.EndInvoke(asyncResult);
          return ((string)(results[0]));
      }
  
      /// &lt;remarks/&gt;
      public void SayHelloAsync() {
          this.SayHelloAsync(null);
      }
  
      /// &lt;remarks/&gt;
      public void SayHelloAsync(object userState) {
          if ((this.SayHelloOperationCompleted == null)) {
              this.SayHelloOperationCompleted = new System.Threading.SendOrPostCallback(this.OnSayHelloOperationCompleted);
          }
          this.InvokeAsync("SayHello", new object[0], this.SayHelloOperationCompleted, userState);
      }
  
      private void OnSayHelloOperationCompleted(object arg) {
          if ((this.SayHelloCompleted != null)) {
              System.Web.Services.Protocols.InvokeCompletedEventArgs invokeArgs = ((System.Web.Services.Protocols.InvokeCompletedEventArgs)(arg));
              this.SayHelloCompleted(this, new SayHelloCompletedEventArgs(invokeArgs.Results, invokeArgs.Error, invokeArgs.Cancelled, invokeArgs.UserState));
          }
      }
  
      /// &lt;remarks/&gt;
      public new void CancelAsync(object userState) {
          base.CancelAsync(userState);
      }
  }
  
  /// &lt;remarks/&gt;
  [System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "4.6.1055.0")]
  public delegate void SayHelloCompletedEventHandler(object sender, SayHelloCompletedEventArgs e);
  
  /// &lt;remarks/&gt;
  [System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "4.6.1055.0")]
  [System.Diagnostics.DebuggerStepThroughAttribute()]
  [System.ComponentModel.DesignerCategoryAttribute("code")]
  public partial class SayHelloCompletedEventArgs : System.ComponentModel.AsyncCompletedEventArgs {
  
      private object[] results;
  
      internal SayHelloCompletedEventArgs(object[] results, System.Exception exception, bool cancelled, object userState) : 
              base(exception, cancelled, userState) {
          this.results = results;
      }
  
      /// &lt;remarks/&gt;
      public string Result {
          get {
              this.RaiseExceptionIfNecessary();
              return ((string)(this.results[0]));
          }
      }
  }
  </code></pre>
  
  <p>I found this discussion and, I think, it is what is needed in my case:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/16028014/how-can-i-pass-a-username-password-in-the-header-to-a-soap-wcf-service"><strong>How can I pass a username/password in the header to a SOAP WCF Service</strong></a></li>
  </ul>
  
  <p>I want to pass credentials in the SOAP header of the message. How to do it?</p>
  
  <p>If I use this code:</p>
  
  <pre><code>using (UsrService client = new UsrService())
  {
      client.ClientCredentials.UserName.UserName = "user";
      client.ClientCredentials.UserName.Password = "password";
      client.SayHello();
  }
  </code></pre>
  
  <p>I get this error message:</p>
  
  <pre><code>"UsrService does not contain a difinition for ClientCredentials"
  </code></pre>
  
  <p>Should I include the missing code to the generated proxy class?</p>