问题描述
我有一个运行Windows Server的WebServer WCF .NET4.6.2应用程序。我只想从同一服务器内调用该WebServer的API,因此防火墙会阻止我的WebServer的端口。我也想只允许具有管理员权限的用户调用WebServer 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();
我已经创建了一个根证书,并将其放入LocalComputer \ Trusted Root CA中。
我已经创建了一个中间证书,并放入了LocalComputer \ Intermediate CA。
我创建了两个[证书+私钥]-名称分别为“ localhost”和“ client”。
我已将“ localhost”放入LocalComputer \ Personal
我已将“客户端”放置到LocalComputer \ Personal和MyUser \ Trusted People。
我的WebServer以MyUser身份运行。我注册了Web服务器以使用SSL。
netsh http add sslcert ipport=0.0.0.0:2000 certhash="hash of localhost" appid="{00112233-4455-6677-8899-AABBCCDDEEFF}" clientcertnegotiation=enable
我试图在使用MyUser运行WebServer的同一主机上运行以下命令
Invoke-WebRequest -method post -uri https://localhost:2000/ping -CertificateThumbprint "hash of client"
但是我得到的是
Invoke-WebRequest : The Remote Server returned an error: (403) Forbidden.
提琴手显示响应
System.Security.Authentication.AuthenticationException The remote certificate is invalid according to the validation procedure
将binding.Security.Transport.ClientCredentialType更改为HttpClientCredentialType.None无效,并且证明我的客户端可以验证我的Web服务器的证书,但是我不知道为什么Web服务器无法验证客户端的证书。 任何帮助将不胜感激。
解决方法
我找到了解决方案,我错过了两件事:
- 我必须为根CA部署证书吊销列表
- 我必须创建一个包含预期目的“客户端身份验证”的客户端证书