gpt4 book ai didi

c# - 为 SOAP 网络服务传递 WSSE Usernametoken 的正确方式

转载 作者:IT王子 更新时间:2023-10-29 03:47:33 28 4
gpt4 key购买 nike

我正在尝试通过相应的 wsdl 使用 Web 服务。此服务依赖于符合 Web Services Security Basic Security Profile 1.0 的身份验证包括 http://docs.oasis-open.org/wss/2004/01/oasis-200401wss-wssecurity-secext-1.0.xsd 的正确 xmls 命名空间必须包含在请求中。

例子:

<wsse:UsernameToken xmlns:wsse='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd' >
<wsse:Username>
Bob
</wsse:Username>
<wsse:Password Type='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText'>
1234
</wsse:Password>
</wsse:UsernameToken>

我的第一次尝试是沿着 Add Service Reference 的方向定位 wsdl 并从生成的代理中使用它们

ServicePointManager.ServerCertificateValidationCallback = 
(object s, X509Certificate certificate, X509Chain chain,
SslPolicyErrors sslPolicyErrors) => true;

var basicHttpBinding = new BasicHttpBinding(BasicHttpSecurityMode.Transport);
basicHttpBinding.Security.Transport.ClientCredentialType =
HttpClientCredentialType.Basic;

var endpoint = new EndpointAddress("https://secure-ausomxana.crmondemand.com/..."

using (var client = new ContactClient(basicHttpBinding, endpoint))
{

var credential = client.ClientCredentials.UserName;
credential.UserName = "bob";
credential.Password = "1234";

var input = ...
var output = client.ContactQueryPage(input);
}

但是尝试使用 Fiddler 询问 SOAP 消息时,我发现没有添加任何 UsernameToken 元素。

履行契约(Contract)的正确方式是什么?

编辑:根据@John Saunders 的回复,我尝试更改我的代码以使用 wsHttpBinding

var wsHttpBinding = new WSHttpBinding(SecurityMode.Transport);
wsHttpBinding.Security.Transport.ClientCredentialType =
HttpClientCredentialType.Basic;

使用此绑定(bind),SOAP 消息变为

<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing">
<s:Header>
<a:Action s:mustUnderstand="1">document/urn:crmondemand/ws/ecbs/contact/10/2004:ContactQueryPage</a:Action>
<a:MessageID>urn:uuid:17807f44-1fcasfdsfd</a:MessageID>
<a:ReplyTo>
<a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address>
</a:ReplyTo>
<a:To s:mustUnderstand="1">https://secure-ausomxana.crmondemand.com/Services/Integration</a:To>
</s:Header>
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<ContactQueryPage_Input xmlns="urn:crmondemand/ws/ecbs/contact/10/2004">
<ListOfContact xmlns="urn:/crmondemand/xml/Contact/Query">
<Contact>
<Id>1-asdfd</Id>
</Contact>
</ListOfContact>
</ContactQueryPage_Input>
</s:Body>
</s:Envelope>

这添加了 Header 元素,而不是 wsse:UsernameToken 元素,用于引用使用 BasicHttpBinding 的原始 soap 消息是

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<ContactQueryPage_Input xmlns="urn:crmondemand/ws/ecbs/contact/10/2004">
<ListOfContact xmlns="urn:/crmondemand/xml/Contact/Query">
<Contact>
<Id>1-asdfds</Id>
</Contact>
</ListOfContact>
</ContactQueryPage_Input>
</s:Body>
</s:Envelope>

如果我将绑定(bind)更改为

var wsHttpBinding = new WSHttpBinding(SecurityMode.TransportWithMessageCredential);
wsHttpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
wsHttpBinding.Security.Message.ClientCredentialType = MessageCredentialType.UserName;

我得到的 SOAP 消息是

<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing" xmlns:u="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
<s:Header>
<a:Action s:mustUnderstand="1">http://schemas.xmlsoap.org/ws/2005/02/trust/RST/SCT</a:Action>
<a:MessageID>urn:uuid:eeb75457-f29e-4c65-b4bf-b580da26e0c5</a:MessageID>
<a:ReplyTo>
<a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address>
</a:ReplyTo>
<a:To s:mustUnderstand="1">https://secure-ausomxana.crmondemand.com/Services/Integration</a:To>
<o:Security xmlns:o="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" s:mustUnderstand="1">
<u:Timestamp u:Id="_0">
<u:Created>2011-05-02T13:30:09.360Z</u:Created>
<u:Expires>2011-05-02T13:35:09.360Z</u:Expires>
</u:Timestamp>
<o:UsernameToken u:Id="uuid-dc3605a0-6878-42f4-b1f2-37d5c04ed7b4-2">
<o:Username>Bob</o:Username>
<o:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">1234</o:Password>
</o:UsernameToken>
</o:Security>
</s:Header>
<s:Body>
<t:RequestSecurityToken xmlns:t="http://schemas.xmlsoap.org/ws/2005/02/trust">
<t:TokenType>http://schemas.xmlsoap.org/ws/2005/02/sc/sct</t:TokenType>
<t:RequestType>http://schemas.xmlsoap.org/ws/2005/02/trust/Issue</t:RequestType>
<t:Entropy>
<t:BinarySecret u:Id="uuid-7195ad74-580b-4e52-9e2c-682e5a684345-1" Type="http://schemas.xmlsoap.org/ws/2005/02/trust/Nonce">bI4xuyKwZ8OkQYBRnz2LDNV+zhIOnl0nwP24yI1QAwA=</t:BinarySecret>
</t:Entropy>
<t:KeySize>256</t:KeySize>
</t:RequestSecurityToken>
</s:Body>
</s:Envelope>

这似乎非常接近,但这似乎实际上加密了 soap 消息的主体,这是我不希望发生的事情。

如果我指定 wsHttpBinding.Security.Message.ClientCredentialType = MessageCredentialType.UserName; 并且只使用 SecurityMode.Transport 它会返回到它说它匿名的地方。

我无法清除的最后一个障碍是什么?

最终解决方案: 我想我会发布这个以防它对某人有所帮助,除了 UserToken 对象被包装在一个安全节点中之外,这里并没有太大的不同,这是我的服务提供商所要求的,而且似乎是我之前示例的输出是如何生成的。

<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="Contact" closeTimeout="00:01:00" openTimeout="00:01:00"
receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false"
bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferSize="524288" maxBufferPoolSize="524288" maxReceivedMessageSize="524288"
messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="65536" maxArrayLength="131072"
maxBytesPerRead="32768" maxNameTableCharCount="131072" />
<security mode="Transport">
<transport clientCredentialType="None" proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="https://secure-ausomxana.crmondemand.com/Services/Integration"
binding="basicHttpBinding" bindingConfiguration="Contact"
contract="OnDemandContactService.Contact" name="OnDemand.Contact.Endpoint">
<headers>
<wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<wsse:UsernameToken>
<wsse:Username>USERNAME</wsse:Username>
<wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">PASSWORD</wsse:Password>
</wsse:UsernameToken>
</wsse:Security>
</headers>
</endpoint>
</client>
</system.serviceModel>

参见 With C#, WCF SOAP consumer that uses WSSE plain text authentication?了解如何使用代码而不是配置来配置它

最佳答案

如果您需要通过 HTTPS 发送用户名,您可以使用标准方法(如果您的 WSDL 定义正确,这应该通过添加服务引用自动为您创建):

<bindings>
<basicHttpBinding>
<binding name="secured">
<security mode="TransportWithMessageCredential">
<message clientCredentialType="UserName" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint name="..." address="https://..." contract="..." binding="basicHttpBinding"
bindingConfiguration="secured" />
</client>

您可以在代码中定义绑定(bind):

var basicHttpBinding = new BasicHttpBinding(BasicHttpSecurityMode.TransportWithMessageCredential);
basicHttpBinding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName;

您将像现在一样在代理中设置凭据:

client.ClientCredentials.UserName.UserName = "bob";
client.ClientCredentials.UserName.Password = "1234";

如果您只需要通过 HTTP 的 UserNameToken 配置文件而无需任何其他 WS-Security 基础设施,最简单的方法是使用 ClearUserNameBinding .

如果来自客户端的所有请求都需要相同的用户名和密码,您可以使用没有任何安全性的简单 basicHttpBinding 并包含配置中的静态 header :

<client>
<endpoint ...>
<headers>
<wsse:UsernameToken xmlns:wsse='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd' >
<wsse:Username>Bob</wsse:Username>
<wsse:Password Type='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText'>
1234
</wsse:Password>
</wsse:UsernameToken>
</headers>
</endpoint>
</client>

如果您需要更复杂的内容,请显示 WSDL(安全断言)的相关部分或样本 SOAP 请求。另请说明您是否需要使用 HTTP 或 HTTPS。

关于c# - 为 SOAP 网络服务传递 WSSE Usernametoken 的正确方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5836685/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com