使用ClientCredentials的WCF身份验证错误

时间:2014-05-13 07:11:52

标签: c# wcf authentication

我正在尝试连接到WCF服务,但是当我这样做时,我收到此错误:

  

HTTP请求未经授权使用客户端验证方案' Anonymous'。从服务器收到的身份验证标头是   '基本领域=" 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>

1 个答案:

答案 0 :(得分:0)

需要修改配置以在绑定中使用它:

<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的整个想法是它应该是可配置的,这样如果服务发生变化就不需要重新部署代码。