gpt4 book ai didi

wcf - 带证书身份验证的 BasicHttpBinding - 错误 "forbidden"?

转载 作者:行者123 更新时间:2023-12-04 11:17:54 33 4
gpt4 key购买 nike

我正在尝试使用 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();

本地主机的证书位于 Personal、Trusted Root 和 Trusted 3rd Party 容器中。 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)禁止。”

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

最佳答案

尝试在设置 Security.Mode 后立即在客户端中添加它:

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

关于wcf - 带证书身份验证的 BasicHttpBinding - 错误 "forbidden"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/731379/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com