Asp.net 为IIS上托管的WCF服务实现自签名X509证书
在开始讨论这个问题之前,让我先解释一下我在寻找什么,我做了什么 我创建了一个自签名X509证书,一个用于客户端,另一个用于服务器。 我已经创建了一个WCF服务,并将Web.Config配置为使用该证书。 我甚至配置了客户机Web.Config,以基于证书的安全性发出请求。 在自托管上一切都很好 然而,当我在IIS上部署服务时,我遇到了一大堆问题,我一直在寻找解决方案,但什么都没有解决 如果有人能提供可能的解决方案,那就太好了 下面是服务器和客户端的配置文件 服务器(WCF服务)Web.config:Asp.net 为IIS上托管的WCF服务实现自签名X509证书,asp.net,wcf,c#-4.0,iis,ssl,Asp.net,Wcf,C# 4.0,Iis,Ssl,在开始讨论这个问题之前,让我先解释一下我在寻找什么,我做了什么 我创建了一个自签名X509证书,一个用于客户端,另一个用于服务器。 我已经创建了一个WCF服务,并将Web.Config配置为使用该证书。 我甚至配置了客户机Web.Config,以基于证书的安全性发出请求。 在自托管上一切都很好 然而,当我在IIS上部署服务时,我遇到了一大堆问题,我一直在寻找解决方案,但什么都没有解决 如果有人能提供可能的解决方案,那就太好了 下面是服务器和客户端的配置文件 服务器(WCF服务)Web.confi
<configuration>
<appSettings />
<connectionStrings />
<system.web>
<compilation debug="true" targetFramework="4.0" />
<authentication mode="Windows" />
</system.web>
<system.web.extensions>
<scripting>
<webServices/>
</scripting>
</system.web.extensions>
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="wsHttpEndpointBinding">
<security>
<message clientCredentialType="Certificate" />
</security>
</binding>
</wsHttpBinding>
</bindings>
<services>
<service name="WCFServiceCertificate.Service1" behaviorConfiguration="WCFServiceCertificate.Service1Behavior">
<endpoint address="https://***.***.**.**:91" binding="wsHttpBinding" bindingConfiguration="wsHttpEndpointBinding" contract="WCFServiceCertificate.IService1">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="WCFServiceCertificate.Service1Behavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
<serviceCredentials>
<clientCertificate>
<authentication certificateValidationMode="PeerTrust" />
</clientCertificate>
<serviceCertificate findValue="WCfServer" storeLocation="LocalMachine" storeName="My" x509FindType="FindBySubjectName" />
</serviceCredentials>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
<system.webServer>
<directoryBrowse enabled="true" />
</system.webServer>
</configuration>
客户端配置:
<?xml version="1.0"?>
<configuration>
<appSettings/>
<connectionStrings/>
<system.web>
<compilation debug="false" targetFramework="4.0"/>
<authentication mode="Windows"/>
<pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID"/>
</system.web>
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="WSHttpBinding_IService1" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text"
textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<reliableSession ordered="true" inactivityTimeout="00:10:00"
enabled="false" />
<security mode="Message">
<transport clientCredentialType="Windows" proxyCredentialType="None"
realm="" />
<message clientCredentialType="Certificate" negotiateServiceCredential="true"
algorithmSuite="Default" />
</security>
</binding>
</wsHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:1387/Service1.svc" binding="wsHttpBinding"
bindingConfiguration="WSHttpBinding_IService1" contract="ServiceReference1.IService1"
name="WSHttpBinding_IService1" behaviorConfiguration="CustomBehavior">
<identity>
<dns value="WCfServer" />
</identity>
</endpoint>
</client>
<behaviors>
<endpointBehaviors>
<behavior name="CustomBehavior">
<clientCredentials>
<clientCertificate findValue="WcfClient" x509FindType="FindBySubjectName" storeLocation="CurrentUser" storeName="My"/>
<serviceCertificate>
<authentication certificateValidationMode="PeerTrust"/>
</serviceCertificate>
</clientCredentials>
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
浏览服务时输出屏幕截图
浏览器
感谢您的帮助……提前感谢各位。您需要在受信任的证书颁发机构下创建客户端证书和服务器证书。客户端和服务器都必须信任创建证书的证书颁发机构。
如果您没有CA或没有钱从公共CA购买证书,您可以设置一个工作的本地环境:
-创建根CA证书:rootCA
-在服务器和客户端的受信任根证书存储库中安装rootCA(发布密钥)
-从rootCA创建https证书并将其安装在IIS上
-创建客户端证书并在客户端上安装它们 从上一个关于stackoverflow的问题开始
我写了一个循序渐进的指南:
谢谢大家的支持。
唯一的问题是该证书不是在受信任的安全性下创建的,它是一个正常的自签名证书,浏览器不接受该证书 当在受信任的安全性下创建证书时,问题得到了解决。 协同配置设置保持良好
最后感谢@Fabrizio的大力帮助。问题出在哪里?你有例外吗?我没有例外,但我无法浏览服务。我将附上从浏览器访问时得到的响应的屏幕截图。感谢Fabrizio提供的有用链接和资料。
我的配置在托管环境中运行良好,尽管我意识到客户端计算机不接受证书,因为它不受信任的安全性。除此之外,一切都很顺利。再次感谢你。