使用ClientCredentials的WCF身份验证错误
本文关键字:身份验证 错误 WCF ClientCredentials 使用 | 更新日期: 2023-09-27 18:03:16
我正在尝试连接到WCF服务,但当我这样做时,我得到这个错误:
HTTP请求未经授权,客户端身份验证方案为"匿名"。日志含义从服务器接收到的认证头为的基本领域="ServiceGateway"。
当我在WCF客户端上设置客户端凭据时,我知道我在进行身份验证:
new wcfClient(enpointconfigurationName, url)
{
ClientCredentials =
{
UserName =
{
UserName = "yyy",
Password = "zzz"
}
}
}
编辑
我在web.config中有这个WCF配置:
<client>
<endpoint address="xxx" binding="basicHttpBinding" bindingconfiguration="myBinding" contract="yyy" name="myName" />
</client>
<basichttpbinding>
<binding name="myBinding" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:01:00" sendTimeout="00:01:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None" realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
</basichttpbinding>
需要修改配置以在绑定中使用:
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Basic" proxyCredentialType="None" realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
编辑
之所以有效,是因为即使在代码中设置了UserName属性,WCF服务仍然需要配置以发送凭据。这是在WCF ABC的B中完成的(http://msdn.microsoft.com/en-us/library/aa480190.aspx):
)"A" stands for Address: Where is the service?
"B" stands for Binding: How do I talk to the service?
"C" stands for Contract: What can the service do for me?
WCF的整个思想是它应该是可配置的,这样如果服务发生变化,就不需要重新部署代码。