title: Client for a WSSE-based service using C#
body: <p>I have recently been writing a client for a WSS service. It is using the usernametoken profile according to it's documentation. And I have written something like this as a code to communicate it. But due to not allowing of anonymous authentication I have always faced the unauthorized error by the service.<br>
  How can I work this out?</p>
  
  <p>Here is the sample code:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.IO;
  using System.Linq;
  using System.Net;
  using System.Text;
  using System.Threading.Tasks;
  using System.Windows.Forms;
  using System.Xml;
  
  namespace WindowsFormsApplication1
  {
      public partial class Form1 : Form
      {
          public Form1()
          {
              InitializeComponent();
          }
  
          private void button1_Click(object sender, EventArgs e)
          {
              Execute();
          }
          public  void Execute()
          {
              HttpWebRequest request = CreateWebRequest();
              XmlDocument soapEnvelopeXml = new XmlDocument();
              soapEnvelopeXml.LoadXml(@"&lt;?xml version=""1.0"" encoding=""UTF-8""?&gt;
  &lt;soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/""&gt;
  &lt;soapenv:Header&gt;
  &lt;wsse:Security xmlns:wsse=""http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd""&gt;
  &lt;wsse:UsernameToken&gt;
  &lt;wsse:Username&gt;something&lt;/wsse:Username&gt;
  &lt;wsse:Password&gt;something&lt;/wsse:Password&gt;
  &lt;/wsse:UsernameToken&gt;
  &lt;/wsse:Security&gt;
  &lt;/soapenv:Header&gt;
  &lt;soapenv:Body&gt;
  ...
  &lt;/soapenv:Body&gt;
  &lt;/soapenv:Envelope&gt;
  ");
              using (Stream stream = request.GetRequestStream())
              {
                  soapEnvelopeXml.Save(stream);
              }
  
              using (WebResponse response = request.GetResponse())
              {
                  using (StreamReader rd = new StreamReader(response.GetResponseStream()))
                  {
                      string soapResult = rd.ReadToEnd();
                      Console.WriteLine(soapResult);
                  }
              }
          }
          public HttpWebRequest CreateWebRequest()
          {
              HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(@"http://127.0.0.1:5434");
              webRequest.Headers.Add(@"SOAP:Action");
              webRequest.ContentType = "text/xml;charset=\"utf-8\"";
              webRequest.Accept = "text/xml";
              webRequest.Method = "POST";
              return webRequest;
          }
      }
  }
  </code></pre>
  
  <p>Any help is appreciated.</p>