行。我通过HttpWebRequest调用使用客户端证书运行我的WCF REST服务。但是,如果我使用此代码它不起作用:
ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, errors) => true;
var factory = new WebChannelFactory<IMyContract>(new Uri(MyUrl));
var cc = factory.Endpoint.Behaviors.Find<ClientCredentials>();
cc.ClientCertificate.SetCertificate(StoreLocation.LocalMachine, StoreName.My, X509FindType.FindBySubjectName,MySubjectName);
cc.ServiceCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.None;
cc.ServiceCertificate.Authentication.RevocationMode = X509RevocationMode.NoCheck;
cc.ServiceCertificate.Authentication.TrustedStoreLocation = StoreLocation.LocalMachine;
cc.ServiceCertificate.SetDefaultCertificate(StoreLocation.LocalMachine, StoreName.My, X509FindType.FindBySubjectName, MyOtherSubjectName);
_svc = factory.CreateChannel();
var result1 = _svc.Post(GetMyPostObject());
我收到错误:
The HTTP request was forbidden with client authentication scheme 'Anonymous'.
但使用HttpWebRequest
使用完全相同的客户端证书在完全相同的URL 上调用完全相同的服务可以正常工作。
我也尝试过没有所有ServiceCertificate的东西,但它没有任何区别 有人可以告诉我,我的WCF代码错过了什么吗?