如何为WS-Security生成UsernameToken?

时间:2019-05-07 06:56:17

标签: c# .net web-services soap ws-security

我有一些Web服务(用Java写),我需要在.NET中为其创建客户端。 WebService具有WS-Security,并且需要PasswordDigest。 首先,我已经在SoapUI中对其进行了测试,并且它适用于:

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)

<soap:Envelope xmlns:not="http://existing-domain.com/op/integration/services" xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
   <soap:Header>
    <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">
        <wsse:UsernameToken wsu:Id="UsernameToken-2C1E2DE2B61EBB94E115572099598331">
            <wsse:Username>SOME_LOGIN</wsse:Username>
            <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest">rcSb6Hd8btcI9g6JvO7dGdiTBTI=</wsse:Password>
            <wsse:Nonce EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">PCoVwJm9oEXtusx6gkMb7w==</wsse:Nonce>
            <wsu:Created>2019-05-07T06:19:19.824Z</wsu:Created>
        </wsse:UsernameToken>
    </wsse:Security>
   </soap:Header>
   <soap:Body>
      <not:someMethod/>
   </soap:Body>
</soap:Envelope>

下一步,我在.NET中准备了简单的客户端,并通过Wireshark检查它发送的内容:

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

<soap:Envelope xmlns:not="http://existing-domain.com/op/integration/services" xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
  <soap:Header>
    <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">
      <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">
        <wsse:Username>SOME_LOGIN</wsse:Username>
        <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest">g112a9eHPR1hXD4UH+Lh3o8JV/o=</wsse:Password>
        <wsse:Nonce>EiUbo2DbXMGhM26fT+ZkJQ==</wsse:Nonce>
        <wsu:Created>2019-05-07T06:27:59Z</wsu:Created>
      </wsse:UsernameToken>
    </wsse:Security>
  </soap:Header>
  <soap:Body>
    <not:someMethod />
  </soap:Body>
</soap:Envelope>

不幸的是,我一直得到500个状态代码和响应:

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
    <soap:Body>
        <soap:Fault>
            <soap:Code>
                <soap:Value>soap:Sender</soap:Value>
                <soap:Subcode>
                    <soap:Value xmlns:ns1="http://ws.apache.org/wss4j">ns1:SecurityError</soap:Value>
                </soap:Subcode>
            </soap:Code>
            <soap:Reason>
                <soap:Text xml:lang="en">A security error was encountered when verifying the message</soap:Text>
            </soap:Reason>
        </soap:Fault>
    </soap:Body>
</soap:Envelope>

我很确定它是由于授权问题而出现的(如果提供错误的凭据,我在SoapUI中也有相同的消息)。

我的客户端中的部分是使用以下方式创建的:

UsernameToken t = new UsernameToken("SOME_LOGIN", "SOME_PASSWORD", PasswordOption.SendHashed);
string usernameTokenSection = t.GetXml(new XmlDocument()).OuterXml.ToString();

关于如何创建PasswordDigits( Base64(SHA1(password + nonce + created))),Nonce( Base64(RandomString))或创建日期的一些文章但我找不到wsu:Id="UsernameToken-2C1E2DE2B61EBB94E115572099598331"是什么以及如何创建它。上面从UsernameToken获取xml的代码返回完整部分,因此我决定使用它,但我注意到它附加了wsu:Id="SecurityToken-85707168-b5c6-47dc-93e9-45afa466fa2a"(而不是wsu:Id="UsernameToken-2C1E2DE2B61EBB94E115572099598331")。我可以更改名称并删除-字符,但没有任何更改,并且仍然收到500 Internal Server Error,消息为“ 验证消息时遇到安全错误”。

所以,我的问题是如何使用正确的wsu:Id="UsernameToken-XXXXXXXXXXXXXXX"数据生成UsernameToken部分?是什么-只是随机字符串或根据用户名和密码创建的一些哈希?

1 个答案:

答案 0 :(得分:0)

知道了!我注意到从.NET客户端发送的SOAP在EncodingType节点中没有属性Nonce。在代码中稍作修改后:

UsernameToken t = new UsernameToken("SOME_LOGIN", "SOME_PASSWORD", PasswordOption.SendHashed);
string usernameTokenSection = t.GetXml(new XmlDocument()).OuterXml.ToString().Replace("<wsse:Nonce", "<wsse:Nonce EncodingType=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary\"");

一切正常!

.NET客户端的完整代码:

public static void Execute()
{            
        UsernameToken usernameTokenSection = new UsernameToken("SOME_LOGIN", "SOME_PASSWORD", PasswordOption.SendHashed);

        HttpWebRequest request = CreateWebRequest();
        XmlDocument soapEnvelopeXml = new XmlDocument();
        soapEnvelopeXml.LoadXml(@"<soap:Envelope xmlns:not=""http://existing-domain.com/op/integration/services"" xmlns:soap=""http://www.w3.org/2003/05/soap-envelope"">" +
              "<soap:Header>" +
                @"<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"">"+
                    usernameTokenSection.GetXml(new XmlDocument()).OuterXml.ToString().Replace("<wsse:Nonce", "<wsse:Nonce EncodingType=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary\"") +
                "</wsse:Security>" +
              "</soap:Header>"+
              "<soap:Body>"+
                "<not:someMethod/>"+
              "</soap:Body>"+
            "</soap:Envelope>");

        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 static HttpWebRequest CreateWebRequest()
{
        HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(@"http://192.168.100.101:8181/services/ws/SomeService");

        webRequest.Method = "POST";
        webRequest.Headers.Add("Accept-Encoding:gzip,deflate");
        webRequest.ContentType = "application/soap+xml;charset=UTF-8;action=\"someMethod\"";
        //NOTE: below it's not necessary
        //webRequest.Host = "192.168.100.101:8181";
        //webRequest.KeepAlive = true;
        //webRequest.UserAgent = "Apache-HttpClient/4.1.1 (java 1.5)";

        return webRequest;
}

static void Main(string[] args)
{
        try
        {
            Execute();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        finally
        {
            Console.ReadLine();
        }

}

PS。对于使用UsernameToken类,您必须使用Microsoft.Web.Services2引用,您可以通过Nuget从此处添加它:Microsoft.Web.Services2

PPS。我知道Add Service Reference...有一种更简单的方法,但是它需要其他类和修改才能在Security中创建Header节点,所以我决定发送原始xml并直接对其进行操作。