title: How to generate UsernameToken for WS-Security?
body: <p>I have some web service (wrote in Java) and I need create client for it in .NET. The WebService has WS-Security and need PasswordDigest. 
  First of all, I've tested it in SoapUI and it's works for:</p>
  
  <pre><code>POST http://192.168.100.101:8181/services/ws/SomeService HTTP/1.1
  Accept-Encoding: gzip,deflate
  Content-Type: application/soap+xml;charset=UTF-8;action="someMethod"
  Content-Length: 971
  Host: 192.168.100.101:8181
  Connection: Keep-Alive
  User-Agent: Apache-HttpClient/4.1.1 (java 1.5)
  
  &lt;soap:Envelope xmlns:not="http://existing-domain.com/op/integration/services" xmlns:soap="http://www.w3.org/2003/05/soap-envelope"&gt;
     &lt;soap:Header&gt;
      &lt;wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"&gt;
          &lt;wsse:UsernameToken wsu:Id="UsernameToken-2C1E2DE2B61EBB94E115572099598331"&gt;
              &lt;wsse:Username&gt;SOME_LOGIN&lt;/wsse:Username&gt;
              &lt;wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest"&gt;rcSb6Hd8btcI9g6JvO7dGdiTBTI=&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;PCoVwJm9oEXtusx6gkMb7w==&lt;/wsse:Nonce&gt;
              &lt;wsu:Created&gt;2019-05-07T06:19:19.824Z&lt;/wsu:Created&gt;
          &lt;/wsse:UsernameToken&gt;
      &lt;/wsse:Security&gt;
     &lt;/soap:Header&gt;
     &lt;soap:Body&gt;
        &lt;not:someMethod/&gt;
     &lt;/soap:Body&gt;
  &lt;/soap:Envelope&gt;
  </code></pre>
  
  <p>In the next step I've prepared simple client in .NET and check with Wireshark what it sends:</p>
  
  <pre><code>POST /services/ws/SomeService HTTP/1.1
  Accept-Encoding: gzip,deflate
  Content-Type: application/soap+xml;charset=UTF-8;action="someMethod"
  User-Agent: Apache-HttpClient/4.1.1 (java 1.5)
  Host: 192.168.100.101:8181
  Content-Length: 1124
  Expect: 100-continue
  Connection: Keep-Alive
  
  &lt;soap:Envelope xmlns:not="http://existing-domain.com/op/integration/services" xmlns:soap="http://www.w3.org/2003/05/soap-envelope"&gt;
    &lt;soap:Header&gt;
      &lt;wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"&gt;
        &lt;wsse:UsernameToken xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" wsu:Id="SecurityToken-85707168-b5c6-47dc-93e9-45afa466fa2a" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"&gt;
          &lt;wsse:Username&gt;SOME_LOGIN&lt;/wsse:Username&gt;
          &lt;wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest"&gt;g112a9eHPR1hXD4UH+Lh3o8JV/o=&lt;/wsse:Password&gt;
          &lt;wsse:Nonce&gt;EiUbo2DbXMGhM26fT+ZkJQ==&lt;/wsse:Nonce&gt;
          &lt;wsu:Created&gt;2019-05-07T06:27:59Z&lt;/wsu:Created&gt;
        &lt;/wsse:UsernameToken&gt;
      &lt;/wsse:Security&gt;
    &lt;/soap:Header&gt;
    &lt;soap:Body&gt;
      &lt;not:someMethod /&gt;
    &lt;/soap:Body&gt;
  &lt;/soap:Envelope&gt;
  </code></pre>
  
  <p>Unfortunatelly I allways got 500 status code and response:</p>
  
  <pre><code>&lt;soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope"&gt;
      &lt;soap:Body&gt;
          &lt;soap:Fault&gt;
              &lt;soap:Code&gt;
                  &lt;soap:Value&gt;soap:Sender&lt;/soap:Value&gt;
                  &lt;soap:Subcode&gt;
                      &lt;soap:Value xmlns:ns1="http://ws.apache.org/wss4j"&gt;ns1:SecurityError&lt;/soap:Value&gt;
                  &lt;/soap:Subcode&gt;
              &lt;/soap:Code&gt;
              &lt;soap:Reason&gt;
                  &lt;soap:Text xml:lang="en"&gt;A security error was encountered when verifying the message&lt;/soap:Text&gt;
              &lt;/soap:Reason&gt;
          &lt;/soap:Fault&gt;
      &lt;/soap:Body&gt;
  &lt;/soap:Envelope&gt;
  </code></pre>
  
  <p>I'm pretty sure it's appear because of authorization problem (I had same message in SoapUI if provided wrong credentials).</p>
  
  <p>The section  in my client is created with:</p>
  
  <pre><code>UsernameToken t = new UsernameToken("SOME_LOGIN", "SOME_PASSWORD", PasswordOption.SendHashed);
  string usernameTokenSection = t.GetXml(new XmlDocument()).OuterXml.ToString();
  </code></pre>
  
  <p>There is a few articels how to create PasswordDigits (<em>Base64( SHA1(password + nonce + created) )</em>), Nonce (<em>Base64(RandomString)</em>) or Created date but I can't find what is <code>wsu:Id="UsernameToken-2C1E2DE2B61EBB94E115572099598331"</code> and how to create it. Above code where I get xml from UsernameToken returns complete section so I decided to use it, but I noticed that it append <code>wsu:Id="SecurityToken-85707168-b5c6-47dc-93e9-45afa466fa2a"</code> (instead of <code>wsu:Id="UsernameToken-2C1E2DE2B61EBB94E115572099598331"</code>). I can change the name and remove <code>-</code> characters but it's nothing changed and I still get 500 Internal Server Error with message "<strong>A security error was encountered when verifying the message</strong>".</p>
  
  <p>So, my question is how to generate UsernameToken section with proper <code>wsu:Id="UsernameToken-XXXXXXXXXXXXXXX"</code>data? What is it - just random string or some hash created based on username and password?</p>