我需要使用x509證書從富客戶端通過Internet將安全的消息級別身份驗證發送到安全的WCF Web服務。如何配置WCF通過互聯網使用x509證書?
具體而言,我正在尋找有關設置,配置,編碼和部署的分步指南,包括創建「開發」證書,安裝它並獲取「真實」生產證書。
我需要使用x509證書從富客戶端通過Internet將安全的消息級別身份驗證發送到安全的WCF Web服務。如何配置WCF通過互聯網使用x509證書?
具體而言,我正在尋找有關設置,配置,編碼和部署的分步指南,包括創建「開發」證書,安裝它並獲取「真實」生產證書。
下面的步驟是一個指南,幫助您開始:
1)首先,您需要一個Root Authority來生成您的客戶端和服務器證書。您可以使用外部授權機構(例如Verisign),也可以使用Microsoft Certificate Server等軟件生成自己的授權。
要生成發展根授權證書,您可以使用Visual Studio附帶的「makecert」工具,例如,
makecert -n "CN=MyRootCA" -r -sv RootCA.pvk RootCA.cer
2)然後您需要請求/生成您的客戶端和服務器證書。這兩種證書都可以作爲本地機器證書進行安裝,並且都需要使用相同的root權限進行簽名。您可以從Microsoft Certificate Server的Web界面請求客戶端證書,例如http://mycertserver/certsrv
。
要爲每臺機器生成一個開發客戶端證書,您可以再次使用「makecert」。需要注意的是,客戶端證書與第1步
makecert -pe -n "CN=MyCert" -ss my -sky exchange -sk MyCert
-iv MyRootCA.pvk -ic MyRootCA.cer -sr localmachine MyCert.cer
創建這將在其上運行命令的機器上安裝證書,進入個人證書文件夾中的本地計算機存儲發展的根授權證書籤名。
爲了讓服務器信任客戶端證書,您需要在服務器的受信任根證書頒發機構存儲中安裝開發Root Authority證書(使用mmc證書管理單元執行此操作)。客戶還應該以相同的方式安裝根證書,以便他們信任他們自己的證書。
3)將WCF服務配置爲要求使用證書進行客戶端身份驗證(例如,通過web.config)。
<services>
<service
name="TestService"
behaviorConfiguration="wsHttpCertificateBehavior">
<endpoint name="TestEndPoint"
address=""
binding="wsHttpBinding"
bindingConfiguration="wsHttpEndpointBinding"
contract="TestService.IMyContract">
<identity>
<dns value=""/>
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange"/>
</service>
</services>
<bindings>
<wsHttpBinding>
<binding name="wsHttpEndpointBinding">
<security mode="Message">
<message clientCredentialType="Certificate"/>
</security>
</binding>
</wsHttpBinding>
</bindings>
<behaviors>
<behavior name="wsHttpCertificateBehavior">
<serviceMetadata httpGetEnabled="false" httpsGetEnabled="true"/>
<serviceCredentials>
<clientCertificate>
<authentication
certificateValidationMode="PeerOrChainTrust"
revocationMode="NoCheck"/>
</clientCertificate>
<serverCertificate findValue="CN=MyCert"/>
</serviceCredentials>
</behavior>
</behaviors>
4)現在配置調用者(例如通過app.config)。
<client>
<endpoint name="wsHttpBinding"
address="https://localhost/TestService/TestService.svc"
binding="wsHttpBinding"
bindingConfiguration="wsHttpBinding"
behaviorConfiguration="wsHttpCertificateBehavior"
contract="TestService.IMyContract">
<identity>
<dns value="MyCert"/>
</identity>
</endpoint>
</client>
<bindings>
<wsHttpBinding>
<binding name="wsHttpBinding">
<security mode="Message">
<message clientCredentialType="Certificate"/>
</security>
</binding>
</wsHttpBinding>
</bindings>
<behaviors>
<endpointBehaviors>
<behavior name="wsHttpCertificateBehavior">
<clientCredentials>
<clientCertificate findValue="MyCert" storeLocation="LocalMachine"/>
<serviceCertificate>
<authentication
certificateValidationMode="PeerOrChainTrust"
revocationMode="NoCheck"
trustedStoreLocation="LocalMachine"/>
</serviceCertificate>
</clientCredentials>
</behavior>
</endpointBehaviors>
</behaviors>
上面的XML不正確。不幸的是,我的編輯被拒絕了。如果遇到問題,請注意``serverCredentials>`應該是``,並且缺少``元素。至少在.NET 4.5中是這樣。請參閱http://stackoverflow.com/review/suggested-edits/7584410 –
Jimothy
2015-04-10 04:05:49