title: how to add SOAP Security header
body: <p>I've read a lot of articles and answers but I couldn't work it out.</p>
  
  <p>I'm using .NET Framework 4.0 on my project. So, I first added the WebService as a Service Reference and get the bindings on my app.config. And I'm going to be listing my attempts</p>
  
  <hr>
  
  <p><strong>Attempt #1</strong></p>
  
  <p>I instantiated the service like this and added the credentials:</p>
  
  <p>On App.Config</p>
  
  <pre><code>&lt;binding name="SubscriptionWSImplServiceSoapBinding" &gt;
    &lt;security mode="TransportWithMessageCredential"&gt;
      &lt;transport clientCredentialType="Basic"/&gt;
    &lt;/security&gt;
  &lt;/binding&gt;
  
  SubscriptionWSImplServiceClient ClientWs = new SubscriptionWSImplServiceClient();
  ClientWs.ClientCredentials.UserName.UserName = "some-username";
  ClientWs.ClientCredentials.UserName.Password = "some-password";
  
  var SomeResposne = ClientWs.RunMethod(); //I have exception at this point
  </code></pre>
  
  <p>I get an Exception as below:</p>
  
  <pre><code> System.ArgumentException: The provided URI scheme 'http' is invalid;
   expected 'https'. Parameter name: via at System.ServiceModel.Channels
  </code></pre>
  
  <p>The exceptions says that the web service's url must be HTTPS but the given web service url to me is HTTP not HTTPS.</p>
  
  <p>But I did not mind the HttpS and went for my 2nd attempt.</p>
  
  <hr>
  
  <p><strong>Attempt #2</strong></p>
  
  <p>As stated in <a href="https://stackoverflow.com/questions/7247536/creating-headers-wsse-section-of-wcf-client-programatically-in-c-sharp">this answer</a>  I tried as below:</p>
  
  <pre><code>BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.TransportWithMessageCredential);
  binding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName;
  CustomBinding customBinding = new CustomBinding(binding);
  SecurityBindingElement element = customBinding.Elements.Find&lt;SecurityBindingElement&gt;();
  element.IncludeTimestamp = false;
  
  EndpointAddress address = new EndpointAddress("https://www.GivenServiceUrl.com/WebService"); //Note that the url does not end as ..svc or ..asmx! Just the name like this with no dot or extension
  
  
  SubscriptionWSImplServiceClient ClientWs = new SubscriptionWSImplService(customBinding, address);
  ClientWs.ClientCredentials.UserName.UserName = "some-username";
  ClientWs.ClientCredentials.UserName.Password = "some-password";
  
  var SomeResposne = ClientWs.RunMethod(); //I have exception at this point
  </code></pre>
  
  <p>I get an Exception as below:</p>
  
  <blockquote>
    <p>System.ServiceModel.Security.SecurityNegotiationException: Could not
         establish trust relationship for the SSL/TLS secure channel with authority '<a href="https://www.GivenServiceUrl.com/WebService" rel="nofollow noreferrer">https://www.GivenServiceUrl.com/WebService</a>'</p>
  </blockquote>
  
  <p>Therefore, I went for my 3rd attempt which I gave the reference as WebReference not as ServiceReference on my project.</p>
  
  <hr>
  
  <p><strong>Attempt #3</strong></p>
  
  <pre><code>SubscriptionWSImplService ClientWs2 = new SubscriptionWSImplService();
  var SomeResposne = ClientWs.RunMethod(); //I have exception at this point
  </code></pre>
  
  <p>I don't know how to send the security header at this point.</p>
  
  <p>I get an Exception as below:</p>
  
  <pre><code> System.Web.Services.Protocols.SoapHeaderException: An error was
   discovered processing the &lt;wsse: Security&gt; header
  </code></pre>
  
  <p>But in here, I could not add the necessary header. Could you help me with this?</p>
  
  <hr>
  
  <p><strong>Attempt 1 or Attempt 2 or Attempt 3</strong>, which one should be used also considering that I use Framework 4.0? And how can I solve the problem?</p>
  
  <p>According to document the final request SOAP must be looking like this:</p>
  
  <pre><code>&lt;soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sub="http://subscription.services.ws.fourplay.com.tr/"&gt;
     &lt;soapenv:Header&gt;
        &lt;wsse:Security soapenv:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"&gt;
           &lt;wsse:UsernameToken wsu:Id="UsernameToken-12" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"&gt;
              &lt;wsse:Username&gt;admin&lt;/wsse:Username&gt;
              &lt;wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText"&gt;123456&lt;/wsse:Password&gt;
             &lt;wsse:Nonce EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary"&gt;1lcj+WbCMlrPyhcud4QxiQ==&lt;/wsse:Nonce&gt;
             &lt;wsu:Created&gt;2012-06-05T10:23:26.879Z&lt;/wsu:Created&gt;
          &lt;/wsse:UsernameToken&gt;
       &lt;/wsse:Security&gt;
     &lt;/soapenv:Header&gt;
     &lt;soapenv:Body&gt;
        &lt;sub:RunMethod&gt;
           &lt;!--Optional:--&gt;
           &lt;sub:opaque id&gt;555555555&lt;/sub:opaque id&gt;
           &lt;sub:params&gt;
              &lt;!--Optional:--&gt;
              &lt;name&gt;GUID&lt;/name&gt;
              &lt;!--Optional:--&gt;
              &lt;value&gt;2fc4ce1d-645e-41f4-811e-28510a02a17f &lt;/value&gt;
           &lt;/sub:params&gt;      
  &lt;/sub: RunMethod &gt;
  </code></pre>