我正在尝试通过相应的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>
我的第一次尝试是针对wsdl的Add Service Reference
以及使用它们生成的代理
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元素。
履行此合同的正确方法是什么?
编辑:跟随@John Saunders的回复我试图改变我的代码以使用wsHttpBinding
var wsHttpBinding = new WSHttpBinding(SecurityMode.Transport);
wsHttpBinding.Security.Transport.ClientCredentialType =
HttpClientCredentialType.Basic;
使用此绑定,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>
如果我将绑定更改为
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>
这似乎非常接近但是这似乎实际上加密了肥皂消息的主体,这是我不想发生的事情。
如果我仅使用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?了解如何使用代码配置它而不是配置
答案 0 :(得分:57)
如果您需要通过HTTPS发送UserName,您可以使用标准方法(如果您的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>
您可以在代码中定义绑定:
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而不需要任何安全性,并且包括配置中的静态头:
<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,请同时提及。
答案 1 :(得分:7)
@Ladislav的回答是正确的。但是,我正在尝试使用{1.1}来获取我正在尝试使用的SOAP 1.1 Web服务。在Scott Hanselman的this blog post之后,我能够让它发挥作用。这是我最终使用的代码:
MessageSecurityException
答案 2 :(得分:3)
使用wsHttpBinding,而不是basicHttpBinding。
实际上,您应该只使用“添加服务引用”并指向服务的WSDL。