繁体   English   中英

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

[英]The HTTP request was forbidden with client authentication scheme 'Anonymous'. The remote server returned an error: (403) Forbidden

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

这是合同和服务的实现

[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 检查时没有策略错误,并构建了正确的证书链。

在此处输入图像描述

我的 CA 位于受信任的根目录中,服务器/客户端证书位于 TrustedPeople 存储中。 此外,如果我从浏览器导航到该站点,我会看到返回的页面。 没有错误在此处输入图像描述

我已将 IIS 更新为我认为需要的,将证书绑定在 IIS 中在此处输入图像描述

并通过下面的命令行。在此处输入图像描述

我已将 SSL 设置设置为接受证书在此处输入图像描述

并启用匿名身份验证。在此处输入图像描述

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

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

当您在 IIS 中使用安全类型传输和客户端凭据类型证书托管 WCF 服务时,请将您的客户端证书放在根存储并在 IIS 中启用匿名身份验证 在 IIS 中启用匿名身份验证。 但最重要的是,将您的证书添加到根存储。

如果您运行自托管 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 身份验证

我们收到此错误消息,对我们而言,解决方案是尚未为脚本启用处理程序映射功能权限。 您可以在 IIS 中的 Handler Mappings > Edit Feature Permissions 下启用此功能,或者通过将Script添加到 web.config 中handlers节点的accessPolicy属性:

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

我有这种错误。 该证书是一个子域通配符。 我不得不将私钥导入 LocalMachine 的“受信任的人”商店,这个错误消失了。 就像其他人指出的那样,您也可以尝试将私钥导入 LocalMachine 的“受信任的根”存储。

我之前遇到过这个问题,按照以下步骤帮助解决了这个问题。

在提升中打开一个 cmd 并在命令netsh winhttp set proxy 127.0.0.1:8888下运行

看看这是否解决了问题,并打开了提琴手

稍后使用以下命令重置它netsh winhttp 重置代理

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM