客户端身份验证方案“匿名”禁止 HTTP 请求。远程服务器返回错误:(403) Forbidden

问题描述 投票:0回答:7

我正在尝试创建一个安全的网络服务。

这是合同和服务实施

[ServiceContract()]
public interface ICalculatorService
{
    [OperationContract()]
    int Add(int x, int y);
}

[ServiceBehavior(IncludeExceptionDetailInFaults=true)]
public class CalculatorService : ICalculatorService
{
    public int Add(int x, int y)
    {
        return x + y;
    }
}

这里有服务代码

var b = new WSHttpBinding(SecurityMode.Transport);
b.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate;
b.Security.Message.ClientCredentialType = MessageCredentialType.None;

Type contractType = typeof(ICalculatorService);
Type implementedContract = typeof(CalculatorService);
Uri baseAddress = new Uri("https://localhost:8006/CalculatorService");
ServiceHost sh = new ServiceHost(implementedContract);

sh.AddServiceEndpoint(contractType, b, baseAddress);

//ServiceMetadataBehavior sm = new ServiceMetadataBehavior();
//sm.HttpsGetEnabled = true;
//sm.HttpsGetUrl = new Uri("https://localhost:8006/CalculatorServiceMex");
//sh.Description.Behaviors.Add(sm);

sh.Credentials.Peer.PeerAuthentication.CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.PeerTrust;
        sh.Credentials.ServiceCertificate.SetCertificate(StoreLocation.LocalMachine, StoreName.TrustedPeople, X509FindType.FindBySubjectName, "localhost");

sh.Open();
Console.WriteLine("Service is Listening");
Console.ReadLine();
sh.Close();

这是客户端代码

var b = new WSHttpBinding(SecurityMode.Transport);
b.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate;
b.Security.Message.ClientCredentialType = MessageCredentialType.None;

var factory = new ChannelFactory<ICalculatorService>(b);
factory.Credentials.Peer.PeerAuthentication.CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.PeerTrust;
        factory.Credentials.ClientCertificate.SetCertificate(StoreLocation.LocalMachine, StoreName.TrustedPeople, X509FindType.FindBySubjectName, "localhost");

var client = factory.CreateChannel(new EndpointAddress(new Uri("https://localhost:8006/CalculatorService")));

ServicePointManager.ServerCertificateValidationCallback =
   ((sender, certificate, chain, sslPolicyErrors) =>
            {
                return true;
            });

ICommunicationObject comObject = client as ICommunicationObject;
int result = -1;
try
{
  comObject.Open();
  result = client.Add(10, 2);
}
catch (Exception ex)
{

}
Console.WriteLine(string.Format("Service say 10 + 2 = {0}", -1));
Console.ReadLine();

服务运行良好,当进行 ServicePointManager.ServerCertificateValidationCallback 检查时,没有策略错误,并且构建了正确的证书链。

enter image description here

我的 CA 位于受信任的根目录中,服务器/客户端证书位于 TrustedPeople 存储中。此外,如果我从浏览器导航到该网站,我会看到返回的页面。没有错误enter image description here

我已将 IIS 更新为我认为需要的内容,并将证书绑定在 IIS 中 enter image description here

并通过下面的命令行。 enter image description here

我已将 SSL 设置设置为接受证书 enter image description here

并启用匿名身份验证。 enter image description here

有谁知道我没有正确执行哪些步骤或发现任何问题?我不断收到相同的错误“客户端身份验证方案‘匿名’禁止 HTTP 请求。”

c# wcf iis ssl
7个回答
5
投票

造成这种情况的另一个原因是您所访问的服务器上的证书本身。确保您已导入私钥。在 MMC 中,这将显示为“友好名称”。这花了我几天的时间才弄清楚。一旦我导入私钥,匿名错误就消失了,一切都很好!


0
投票

当您在 IIS 中使用安全类型传输和客户端凭据类型证书托管 WCF 服务时,将您的客户端证书放在根存储上并在 IIS 中启用匿名身份验证Enable anonymous authentication in IIS. But most importantly, add your certificate to root store.


0
投票

我们收到此错误消息,对于我们来说,解决方案是尚未为脚本启用处理程序映射功能权限。您可以在 IIS 中的“处理程序映射”>“编辑功能权限”下启用此功能,或者通过将

Script
添加到 web.config 中
accessPolicy
节点的
handlers
属性来启用此功能:

<system.webServer>
  <handlers accessPolicy="Script">
    ...
  </handlers>
</system.webServer>

0
投票

如果您运行自托管 WCF 服务(没有 IIS),您只需将以下设置添加到配置文件(在服务器中)即可启用匿名客户端:

<behaviors>
    <serviceBehaviors>
        <behavior name="limitedAuthBehavior">
            <serviceAuthenticationManager authenticationSchemes="Anonymous, Basic, Digest, Negotiate"/>
            <!-- ... -->
        </behavior>
    </serviceBehaviors>
</behaviors>

另外,将 clientCredentialType 设置为“InheritedFromHost”:

<bindings>
      <basicHttpBinding>
        <binding name="secureBinding">
          <security mode="Transport">
            <transport clientCredentialType="InheritedFromHost" />
          </security>
        </binding>
      </basicHttpBinding>
</bindings>

参考资料:

通过 WCF 使用多种身份验证方案

了解 HTTP 身份验证


0
投票

我遇到过这样的错误。该证书是子域通配符证书。我必须将私钥导入 LocalMachine 的“受信任的人”存储中,此错误消失了。正如其他人指出的那样,您还可以尝试将私钥导入 LocalMachine 的“受信任的根”存储中。


0
投票

我之前遇到过这个问题,遵循以下步骤有助于解决该问题。

在提升的环境中打开 cmd 并运行以下命令 netsh winhttp 设置代理 127.0.0.1:8888

看看这是否解决了问题,并打开了fiddler

稍后使用以下命令将其重置回来 netsh winhttp 重置代理


0
投票

有一个注册表项位于

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL
。将该值设置为
0
可以解决该问题。 此处概述了其他一些潜在的解决方案。我有一个现有的答案,它引用了方法 3,并从引用的页面获得了许多赞成票,但由于未知原因被版主删除了。重新发布,以防将来对其他人有帮助。

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.