C# 来自同一主机上Invoke WebRequest的WCF客户端证书身份验证

C# 来自同一主机上Invoke WebRequest的WCF客户端证书身份验证,c#,powershell,wcf,ssl,client-certificates,C#,Powershell,Wcf,Ssl,Client Certificates,我有一个运行Windows服务器的WebServer WCF.NET4.6.2应用程序。我只想从同一台服务器中调用该Web服务器的API,因为防火墙会阻止我的Web服务器的端口。此外,我希望只允许具有管理员权限的用户调用Web服务器API。就这一点而言,我认为证书身份验证会起作用。我的WCF服务设置是 var baseAddress = new Uri("https://localhost:2000/"); var binding = new WebHttpBinding(W

我有一个运行Windows服务器的WebServer WCF.NET4.6.2应用程序。我只想从同一台服务器中调用该Web服务器的API,因为防火墙会阻止我的Web服务器的端口。此外,我希望只允许具有管理员权限的用户调用Web服务器API。就这一点而言,我认为证书身份验证会起作用。我的WCF服务设置是

var baseAddress = new Uri("https://localhost:2000/");
var binding = new WebHttpBinding(WebHttpSecurityMode.Transport);
binding.HostNameComparisonMode = HostNameComparisonMode.Exact;
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate;
var endpoint = new ServiceEndpoint(ContractDescription.GetContract(typeof(MyWebApi)),
    binding,
    new EndpointAddress(baseAddress));    
webServiceHost = new WebServiceHost(kernel.Get<MyWebApi>(), baseAddress);
webServiceHost.AddServiceEndpoint(endpoint);
webServiceHost.Credentials.ClientCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.PeerTrust;
webServiceHost.Credentials.ClientCertificate.Authentication.TrustedStoreLocation = StoreLocation.CurrentUser;
webServiceHost.Description.Behaviors.Find<ServiceDebugBehavior>().HttpHelpPageEnabled = false;
webServiceHost.Open();
我试图在使用MyUser运行Web服务器的同一主机上运行以下命令

Invoke-WebRequest -method post -uri https://localhost:2000/ping -CertificateThumbprint "hash of client"
但我得到的是

Invoke-WebRequest : The remote server returned an error: (403) Forbidden.
Fiddler显示了响应

System.Security.Authentication.AuthenticationException The remote certificate is invalid according to the validation procedure
将binding.Security.Transport.ClientCredentialType更改为HttpClientCredentialType。这些都不起作用,并证明我的客户端可以验证我的Web服务器的证书,但我不明白Web服务器为什么不能验证客户端证书。
任何帮助都将不胜感激。

我找到了解决方案,但我遗漏了两件事:

  • 我必须为我的根CA部署证书吊销列表
  • 我必须创建一个客户端证书,其中包括预期用途的“客户端身份验证”

  • 关于使用证书身份验证的传输安全性,您可以参考此链接:您需要确保服务器信任客户端的证书。有关如何使计算机信任证书,您可以参考此链接:@DingPeng有两个验证1-客户端验证它是否可以信任服务器,在我的示例中为“localhost”证书是由我的中间CA颁发的,这是由我的根CA颁发的。我已将中间CA和根CA添加到信任存储中的相应文件夹中,不确定如何信任其他服务器证书2-服务器验证它是否可以信任客户端。我的“客户”证书由与上述相同的中间CA颁发。我还为受信任的人添加了“客户端”证书。显然我做错了什么,但具体是什么?您可以使用此代码来避免验证证书“ServicePointManager.ServerCertificateValidationCallback+=(o,c,ch,er)=>true;”。
    System.Security.Authentication.AuthenticationException The remote certificate is invalid according to the validation procedure