【问题标题】:BasicHttpBinding with Certificate authentication - error "forbidden"?带有证书身份验证的 BasicHttpBinding - 错误“禁止”?
【发布时间】:2009-04-08 19:00:27
【问题描述】:

我正在尝试使用 BasicHttpBinding 在传输级别使用 SSL 证书相互验证 WCF 服务器和客户端。以下是服务器的创建方式:

var soapBinding = new BasicHttpBinding() { Namespace = "http://test.com" };
soapBinding.Security.Mode = BasicHttpSecurityMode.Transport;
soapBinding.Security.Transport.ClientCredentialType =
    HttpClientCredentialType.Certificate;
var sh = new ServiceHost(typeof(Service1), uri);
sh.AddServiceEndpoint(typeof(IService1), soapBinding, "");
sh.Credentials.ServiceCertificate.SetCertificate(
    StoreLocation.LocalMachine, StoreName.My, 
    X509FindType.FindBySubjectName, "localhost");
sh.Open();

这是客户:

var binding = new BasicHttpBinding();
binding.Security.Mode = BasicHttpSecurityMode.Transport;
var service = new ServiceReference2.Service1Client(binding,
    new EndpointAddress("https://localhost:801/Service1"));

service.ClientCredentials.ClientCertificate.SetCertificate(
    StoreLocation.LocalMachine, StoreName.My, 
    X509FindType.FindBySubjectName, "localhost");

service.ClientCredentials.ServiceCertificate.Authentication.
    CertificateValidationMode =
        System.ServiceModel.Security.X509CertificateValidationMode.PeerTrust;

service.HelloWorld();

localhost 的证书位于个人、受信任的根和受信任的第 3 方容器中。 Internet Explorer 可以连接到主机并查看 WSDL。此外,SSL 调用适用于 ClientCredentialType = HttpClientCredentialType.None

HelloWorld() 失败:

System.ServiceModel.Security.MessageSecurityException occurred<br/>
  Message="The HTTP request was forbidden with client authentication
  scheme 'Anonymous'."

这是一个重新抛出的异常:“远程服务器返回错误:(403) Forbidden。”

如何弄清楚 wtf 是怎么回事?

【问题讨论】:

    标签: wcf web-services


    【解决方案1】:

    尝试在设置Security.Mode之后在客户端添加这个:

    binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate;
    

    【讨论】:

    • 我不敢相信它这么容易,我没有抓住它:(
    • 我有同样的问题,但 app.config 有: 在传输元素中:-(
    • 我正在尝试解决类似的问题 - 我刚刚尝试了您的代码,但“添加服务引用”在客户端上不起作用。我不认为你可以附上你的整个解决方案?
    【解决方案2】:

    如果您使用标准生成的代理类,您可以在App.Config 中将传输客户端凭据类型设置为Certificate

    <binding name="SpoDataServiceSoap">
        <security mode="Transport">
            <transport clientCredentialType="Certificate"></transport>
        </security>
    </binding>
    

    C#

    var client = new MyServiceSoapClient()
    X509Certificate2 cert = CertificateHelper.GetClientCertificate();
    client.ClientCredentials.ClientCertificate.Certificate = cert;
    

    【讨论】:

      猜你喜欢
      • 2018-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-17
      • 1970-01-01
      • 1970-01-01
      • 2018-10-31
      • 1970-01-01
      相关资源
      最近更新 更多