WCF 中基于证书的身份验证
Certificate based authentication in WCF
我正在尝试使用 msdn 示例了解基于证书的身份验证 https://msdn.microsoft.com/en-us/library/ms731074(v=vs.90).aspx
这是服务器代码:
WSHttpBinding binding = new WSHttpBinding();
binding.Security.Mode = SecurityMode.Transport;
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate;
// Create the URI for the endpoint.
Uri httpUri = new Uri("https://localhost/Calculator");
// Create the service and add an endpoint.
ServiceHost myServiceHost = new ServiceHost(typeof(ServiceModel.Calculator), httpUri);
myServiceHost.AddServiceEndpoint(typeof(ServiceModel.ICalculator), binding, "");
// Open the service.
myServiceHost.Open();
Console.WriteLine("Listening...");
Console.ReadLine();
// Close the service.
myServiceHost.Close();
这是我写的客户端代码:
ChannelFactory<ICalculator> factory = null;
WSHttpBinding binding = new WSHttpBinding();
binding.Security.Mode = SecurityMode.Transport;
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate;
EndpointAddress address = new EndpointAddress("https://localhost/Calculator");
factory = new ChannelFactory<ICalculator>(binding, address);
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | SecurityProtocolType.Ssl3;
factory.Credentials.ClientCertificate.SetCertificate(StoreLocation.CurrentUser, StoreName.My, X509FindType.FindBySubjectName, "sroger");
ICalculator channel = factory.CreateChannel();
int y = channel.add(9, 8);
我遇到以下异常:
An unhandled exception of type 'System.ServiceModel.CommunicationException' occurred in mscorlib.dll
Additional information: An error occurred while making the HTTP request to https://localhost/Calculator. This could be due to the fact that the server certificate is not configured properly with HTTP.SYS in the HTTPS case. This could also be caused by a mismatch of the security binding between the client and the server.
我是运行来自同一台机器的客户端和服务器。 "sroger" 是我当前用户中的证书\ personal\certificates 对应于我的机器名称..
不知道从这里开始做什么..有什么想法吗?
在服务器代码中什么证书服务器使用?
谢谢
古鲁马尔
https://msdn.microsoft.com/en-us/library/ms731074(v=vs.90).aspx 您使用的示例不完整。
使用 https wcf 服务需要有效的服务器证书才能工作,在您的情况下,客户端和服务器证书都需要。
这是因为客户端和服务器都需要在 HTTPS 连接中相互信任。
要开始,请阅读 https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/message-security-with-mutual-certificates 这是一个更完整的示例,其中包括指定证书以验证服务。
要通过 https 运行托管 WCF 库,您需要按顺序执行以下操作:
- 使用 X.509 证书配置端口(已被
回答于
)
- 从您的服务器,为您的常用名称创建证书请求
服务器完全限定域名,或至少包括服务器完全限定域名的 DNS subjectAltName。
(有不同的方法可以做到这一点,你可能已经知道了
虽然)
- 颁发证书并在您的服务器上安装证书
- 从托管 WCF 的应用程序的程序集文件中获取应用程序 ID
库(即[程序集:
Guid("5870aeed-caca-4734-8b09-5c0615402bcf")]) 抓取证书
通过查看证书属性来指纹。
以管理员身份打开
CMD 和 运行 这个命令将 X.509 证书绑定到使用的端口
通过您在服务器上的应用
netsh http 添加 sslcert ipport=0.0.0.0:443 certhash=appid={} certstorename=MY
netsh http 添加 iplisten ipaddress=0.0.0.0:443
将此添加到您的服务器代码中:
myServiceHost.Credentials.ServiceCertificate.SetCertificate(StoreLocation.LocalMachine, StoreName.My, X509FindType.FindBySerialNumber, "<certificate thumbprint>");
在您的客户端代码中,通过指定为证书通用名称或主题替代名称的证书的完全限定域名引用您的服务器地址
我正在尝试使用 msdn 示例了解基于证书的身份验证 https://msdn.microsoft.com/en-us/library/ms731074(v=vs.90).aspx
这是服务器代码:
WSHttpBinding binding = new WSHttpBinding();
binding.Security.Mode = SecurityMode.Transport;
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate;
// Create the URI for the endpoint.
Uri httpUri = new Uri("https://localhost/Calculator");
// Create the service and add an endpoint.
ServiceHost myServiceHost = new ServiceHost(typeof(ServiceModel.Calculator), httpUri);
myServiceHost.AddServiceEndpoint(typeof(ServiceModel.ICalculator), binding, "");
// Open the service.
myServiceHost.Open();
Console.WriteLine("Listening...");
Console.ReadLine();
// Close the service.
myServiceHost.Close();
这是我写的客户端代码:
ChannelFactory<ICalculator> factory = null;
WSHttpBinding binding = new WSHttpBinding();
binding.Security.Mode = SecurityMode.Transport;
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate;
EndpointAddress address = new EndpointAddress("https://localhost/Calculator");
factory = new ChannelFactory<ICalculator>(binding, address);
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | SecurityProtocolType.Ssl3;
factory.Credentials.ClientCertificate.SetCertificate(StoreLocation.CurrentUser, StoreName.My, X509FindType.FindBySubjectName, "sroger");
ICalculator channel = factory.CreateChannel();
int y = channel.add(9, 8);
我遇到以下异常:
An unhandled exception of type 'System.ServiceModel.CommunicationException' occurred in mscorlib.dll
Additional information: An error occurred while making the HTTP request to https://localhost/Calculator. This could be due to the fact that the server certificate is not configured properly with HTTP.SYS in the HTTPS case. This could also be caused by a mismatch of the security binding between the client and the server.
我是运行来自同一台机器的客户端和服务器。 "sroger" 是我当前用户中的证书\ personal\certificates 对应于我的机器名称.. 不知道从这里开始做什么..有什么想法吗? 在服务器代码中什么证书服务器使用?
谢谢
古鲁马尔
https://msdn.microsoft.com/en-us/library/ms731074(v=vs.90).aspx 您使用的示例不完整。 使用 https wcf 服务需要有效的服务器证书才能工作,在您的情况下,客户端和服务器证书都需要。 这是因为客户端和服务器都需要在 HTTPS 连接中相互信任。
要开始,请阅读 https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/message-security-with-mutual-certificates 这是一个更完整的示例,其中包括指定证书以验证服务。
要通过 https 运行托管 WCF 库,您需要按顺序执行以下操作:
- 使用 X.509 证书配置端口(已被 回答于 )
- 从您的服务器,为您的常用名称创建证书请求 服务器完全限定域名,或至少包括服务器完全限定域名的 DNS subjectAltName。 (有不同的方法可以做到这一点,你可能已经知道了 虽然)
- 颁发证书并在您的服务器上安装证书
- 从托管 WCF 的应用程序的程序集文件中获取应用程序 ID 库(即[程序集: Guid("5870aeed-caca-4734-8b09-5c0615402bcf")]) 抓取证书 通过查看证书属性来指纹。
以管理员身份打开 CMD 和 运行 这个命令将 X.509 证书绑定到使用的端口 通过您在服务器上的应用
netsh http 添加 sslcert ipport=0.0.0.0:443 certhash=appid={} certstorename=MY
netsh http 添加 iplisten ipaddress=0.0.0.0:443
将此添加到您的服务器代码中:
myServiceHost.Credentials.ServiceCertificate.SetCertificate(StoreLocation.LocalMachine, StoreName.My, X509FindType.FindBySerialNumber, "<certificate thumbprint>");
在您的客户端代码中,通过指定为证书通用名称或主题替代名称的证书的完全限定域名引用您的服务器地址