如何修复“客户端身份验证方案‘匿名’禁止 HTTP 请求”

Fra*_*ser 8 .net c# wcf asp.net-core

我在实现与 WCF 服务通信的客户端时遇到一些问题。它是由另一家公司托管的 WCF,因此我无权访问其代码。我使用 Visual Studio 中的连接服务提供程序工具生成客户端代码,以便我可以发出请求,并且一切都在我的本地计算机上正常运行。我的开发环境遇到问题,如果我尝试向客户端发出请求,则会收到以下错误:

The HTTP request was forbidden with client authentication scheme 'Anonymous'
Run Code Online (Sandbox Code Playgroud)

我一直在查看由提供程序工具生成的客户端代码(有很多代码),我认为它可能与以下代码块有关。

System.ServiceModel.BasicHttpBinding result = new System.ServiceModel.BasicHttpBinding();
result.MaxBufferSize = int.MaxValue;
result.ReaderQuotas = System.Xml.XmlDictionaryReaderQuotas.Max;
result.MaxReceivedMessageSize = int.MaxValue;
result.AllowCookies = true;
result.Security.Mode = System.ServiceModel.BasicHttpSecurityMode.Transport;
return result;
Run Code Online (Sandbox Code Playgroud)

Abr*_*ian 5

此错误通常表示 WCF 服务器使用证书对客户端进行身份验证。当服务器和客户端之间尚未建立信任关系时,就会出现该错误。
在此输入图像描述
一般来说,我们需要提供客户端凭据以供服务器进行身份验证,以便能够调用服务。至于需要提供什么样的凭证,取决于服务器端的绑定信息。

 BasicHttpBinding binding = new BasicHttpBinding();
            binding.Security.Mode = BasicHttpSecurityMode.Transport;
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;
Run Code Online (Sandbox Code Playgroud)

也就是说,上述错误表明服务器使用证书对客户端进行身份验证。

binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate;
Run Code Online (Sandbox Code Playgroud)

关于使用证书对客户端进行身份验证,您可以参考以下链接了解详细信息。
https://learn.microsoft.com/en-us/dotnet/framework/wcf/feature-details/transport-security-with-certificate-authentication
如果有什么我可以帮忙的,请随时告诉我。


Fra*_*ser 5

感谢所有的建议。这实际上只是由我们组织内设置的防火墙规则引起的。一旦删除,代码就会按预期工作。

  • 您删除的防火墙参数是什么? (16认同)