2017-09-15 121 views
0
基于认证

我试图使用MSDN示例https://msdn.microsoft.com/en-us/library/ms731074(v=vs.90).aspx证书在WCF

这是服务器代码来理解基于证书的验证:

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); 

我收到以下例外情况:

类型“System.ServiceModel.CommunicationException”的未处理的异常出现在mscorlib.dll

其他信息:同时使该HTTP请求到https://localhost/Calculator发生错误。这可能是由于在HTTPS情况下服务器证书未使用HTTP.SYS正确配置。这也可能是由于客户端和服务器之间的安全绑定不匹配造成的。

我从同一台机器上运行客户端和服务器。 “sroger”是我当前用户\个人\证书中的证书,它对应于我的机器名称。 不知道该从哪里做什么..任何想法? 在服务器代码中使用什么证书服务器?

谢谢

Gulumal。你用

回答

0

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库工作,你需要做的,以便执行以下操作:

  1. 配置与X.509证书的端口(已 回答 webHttpBinding with certificate
  2. 从您的服务器,创建您的 服务器完全合格的域名,或者,至少包括你的服务器的完全限定域名的DNS的SubjectAltName的通用名称的证书请求。 (有不同的方法来做到这一点,你可能已经知道这个 虽然)
  3. 发出证明书,并从你的应用程序的组件文件托管WCF 库(即[组装服务器上安装证书
  4. 抓斗应用程序ID : 的Guid( “5870aeed-CACA-4734-8b09-5c0615402bcf”)])抓斗的证书 指纹通过查看证书属性。
  5. 作为管理员,打开CMD 并运行此命令绑定X.509证书所使用的端口 通过您的应用程序在服务器上

    的netsh的http添加的sslcert ipport = 0.0.0.0:443 CERTHASH =的appid = {} certstorename = MY

    的netsh的http添加iplisten ip地址= 0.0.0.0: 443

添加到您的服务器代码:

myServiceHost.Credentials.ServiceCertificate.SetCertificate(StoreLocation.LocalMachine, StoreName.My, X509FindType.FindBySerialNumber, "<certificate thumbprint>"); 

在你的客户端代码,由CERT完全限定域名引用您的服务器地址指定为证书的证书通用名或主题替代名称