当前位置:  开发笔记 > 后端 > 正文

带证书身份验证的BasicHttpBinding - 错误"禁止"?

如何解决《带证书身份验证的BasicHttpBinding-错误"禁止"?》经验,为你挑选了1个好方法。

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

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的证书位于Personal,Trusted Root和Trusted 3rd Party容器中.Internet Explorer可以连接到主机并查看WSDL.此外,SSL调用与ClientCredentialType = HttpClientCredentialType.None一起正常工作

HelloWorld()失败:

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

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

如何解决wtf正在进行?



1> 小智..:

设置后,尝试在客户端中添加此项Security.Mode:

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

推荐阅读
php
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有