title: Adding security credentials to SOAP message
body: <p>I need to add a username and password to the http header of a soap message.</p>
  
  <p>I've been following this guide <a href="https://msdn.microsoft.com/en-us/library/ms819938.aspx" rel="nofollow noreferrer">How to: Add Security Credentials to a SOAP Message</a></p>
  
  <p>but it seems to be severely outdated, I tried to implement the changes it suggests and I ended up crashing visual studio, and killing the web server in iis.</p>
  
  <p>I'm using Visual Studio 2015, .net frameword 4.5.1.</p>
  
  <pre><code>string Artifact = Request.QueryString["SAMLart"];
  
                  try
                  {
                      GetAssertionClient client = new GetAssertionClient();
  
                      UsernameToken token = new UsernameToken(settings.GetValue("NetsMID", 2), settings.GetValue("NetsPass", 2), PasswordOption.SendHashed);
  
                      SoapContext requestContext = client.RequestSoapContext;
                      requestContext.Security.Tokens.Add(token);
  
                      RequestType type = new RequestType { ItemsElementName = new ItemsChoiceType3[1] };
                      type.ItemsElementName[0] = ItemsChoiceType3.AssertionArtifact;
                      type.Items = new object[] { Artifact };
  
                      ResponseType resp = client.getassertion(type);
  </code></pre>
  
  <p>this is the code I'm running, of course this is with the modifications to the GetAssertionClient. I had tried to use ClientCredentials earlier, but the response I got from the other side was that mid and password were not included in the post. with this setup the program, visual studio and webserver dies completely, had to reboot computer when it happened.
  did not however have such a dramatic effect second time I tried it, but visual studio still flopped out with ww3w or whatever it's called crashing.</p>
  
  <p>the base of my problem is that the wsdl I used to bind this webservice does not include information about authentication so I need to hack that together manually. Help me, I'm stuck.</p>
  
  <p>edit1:
  I changed the code according a suggested solution below.</p>
  
  <pre><code>string Artifact = Request.QueryString["SAMLart"];
  
                  try
                  {
                      using (var client = new GetAssertionClient())
                      {
                          using (new OperationContextScope((IClientChannel)client.InnerChannel))
                          {
                              WebOperationContext.Current.OutgoingRequest.Headers.Add("Authorization",
                                  Convert.ToBase64String(Encoding.Default.GetBytes(String.Format("{0}:{1}", settings.GetValue("NetsMID", 2), settings.GetValue("NetsPass", 2)))));
  
                              RequestType type = new RequestType { ItemsElementName = new ItemsChoiceType3[1] };
                              type.ItemsElementName[0] = ItemsChoiceType3.AssertionArtifact;
                              type.Items = new object[] { Artifact };
  
                              ResponseType resp = client.getassertion(type);
  </code></pre>
  
  <p>when I reach client.getassertion(type); I get the following exception:</p>
  
  <pre><code>2016-12-16 14:34:10 The communication object, System.ServiceModel.Channels.ServiceChannel, cannot be used for communication because it is in the Faulted state. CommunicationObjectFaultedException mscorlib    
  Server stack trace: 
     at System.ServiceModel.
  </code></pre>
  
  <p>the rest of the stack trace didn't make it in the log. but is there something I missed to implement here? </p>