我正在開發使用SSL和證書進行相互身份驗證的WCF Web服務的概念驗證。帶有相互身份驗證證書的WCF Web服務在客戶端鏈信任驗證上失敗
所以,我有兩個證書都由有效的證書頒發機構提供(這些是生產證書,而不是開發)。下面是鏈和證書的商店位置:
服務器證書鏈
- 發行人的根CA
- 中級1 CA
- 服務器身份驗證證書
我不知道這個細節很重要與否:服務器證書是域通配符證書(* .mydomain.com來)
客戶端證書鏈
- 發行人根CA
- 中間體2 CA
- 客戶端身份驗證證書
發行人根CA是兩個證書共同的根CA。
中間體證書不同。
商店位置
發行人的根CA已導入到受信任的根CA在服務器和客戶機 中間CA 1 & 2已導入到中間CA服務器和客戶端上 頒發者和中間證書只有公鑰。
服務器證書已導入到服務器上的個人。這個證書有一個私鑰。 服務器證書已導入到客戶機上的個人。該證書只有一個公鑰。 客戶端身份驗證證書已導入到服務器和客戶端計算機上的個人。這些證書都有私鑰。
我創建了一個簡單的WCF應用程序項目,託管在IIS 8.5與框架C#4.0中。 我使用在創建項目時默認提供的示例類,我剛剛將其重命名爲DemoService.svc。 然後,我創建了客戶端(我使用winform應用程序讓目標用戶有一個圖形界面來查看結果)並添加Web服務引用。
然後,我修改了服務配置以建立相互認證。所有通過web.config中完成的:
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="demoServiceBehavior">
<serviceAuthorization principalPermissionMode="UseWindowsGroups"></serviceAuthorization>
<serviceMetadata httpGetEnabled="false" httpsGetEnabled="false" />
<serviceDebug includeExceptionDetailInFaults="false" />
<serviceCredentials>
<serviceCertificate storeLocation="LocalMachine" storeName="My" x509FindType="FindBySubjectName" findValue="*.mydomain.com"/>
<clientCertificate>
<authentication certificateValidationMode="ChainTrust" revocationMode="NoCheck" mapClientCertificateToWindowsAccount="true"
trustedStoreLocation="LocalMachine"/>
<certificate storeLocation="LocalMachine" storeName="My" x509FindType="FindBySubjectName"
findValue="myservice.mydomain.com"/>
</clientCertificate>
</serviceCredentials>
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<basicHttpBinding>
<binding name="demoServiceBinding">
<security mode="Transport">
<transport clientCredentialType="Certificate"></transport>
</security>
</binding>
</basicHttpBinding>
</bindings>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true">
<serviceActivations>
<add service="WcfMutualAuthenticationServiceDemo.DemoService" relativeAddress="DemoService.svc" />
</serviceActivations>
</serviceHostingEnvironment>
<services>
<service name="WcfMutualAuthenticationServiceDemo.DemoService" behaviorConfiguration="demoServiceBehavior">
<host>
<baseAddresses>
<add baseAddress="https://myservice.mydomain.com/"/>
</baseAddresses>
</host>
<endpoint name="demoServiceEndpoint"
address=""
binding="basicHttpBinding"
bindingConfiguration="demoServiceBinding"
contract="WcfMutualAuthenticationServiceDemo.IDemoService"></endpoint>
</service>
</services>
</system.serviceModel>
我修改了客戶端配置來建立相互認證過:
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="demoClientBehavior">
<clientCredentials>
<clientCertificate storeLocation="LocalMachine" storeName="My" x509FindType="FindBySubjectName"
findValue="myservice.dekra-automotivesolutions.com"/>
</clientCredentials>
</behavior>
</endpointBehaviors>
</behaviors>
<bindings>
<basicHttpBinding>
<binding name="demoClientBinding">
<security mode="Transport">
<transport clientCredentialType="Certificate"></transport>
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="https://myservice.mydomain.com/DemoService.svc"
behaviorConfiguration="demoClientBehavior"
binding="basicHttpBinding"
bindingConfiguration="demoClientBinding"
contract="DemoServiceValidReference.IDemoService"
name="demoServiceEndpoint" />
</client>
</system.serviceModel>
當我打電話通過客戶端的Web服務,它返回一個例外:
System.ServiceModel.Security.SecurityNegotiationException:無法 建立安全通道的SSL/TLS權威 'myservice.mydomain.com'。 ---> System.Net.WebException:請求已中止:無法創建 SSL/TLS安全通道。在System.Net.HttpWebRequest.GetResponse() 在System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.WaitForReply(時間跨度 超時)---內部異常堆棧跟蹤的結尾---
經過調查,我在服務器的事件查看器中找到一個條目:
處理異常。異常詳細信息: System.IdentityModel.Tokens.SecurityTokenValidationException: X.509證書CN = myservice.mydomain鏈構建失敗。使用的 證書具有無法驗證的信任鏈。 替換證書或更改certificateValidationMode。一個 認證鏈處理正確,但其中一個證書不受策略提供程序的信任。
在 System.IdentityModel.Selectors.X509CertificateChain.Build(X509Certificate2 證書)在 System.IdentityModel.Selectors.X509CertificateValidator.ChainTrustValidator.Validate(X509Certificate2 證書)在 System.IdentityModel.Selectors.X509SecurityTokenAuthenticator.ValidateTokenCore( SecurityToken 令牌)在 System.IdentityModel.Selectors.SecurityTokenAuthenticator.ValidateToken(SecurityToken 令牌)在 System.ServiceModel.Channels.HttpsChannelListener
1.CreateSecurityProperty(X509Certificate2 certificate, WindowsIdentity identity, String authType) at System.ServiceModel.Channels.HttpsChannelListener
1.ProcessAuthentication(IHttpAuthenticationContext authenticationCont EXT)在 System.ServiceModel.Activation.HostedHttpContext.OnProcessAuthentication() 在 System.ServiceModel.Channels.HttpRequestContext.ProcessAuthentication() 在 System.ServiceModel.Channels.HttpChannelListener1.HttpContextReceivedAsyncResult
1.Authenticate() 在 系統。 ServiceModel.Channels.HttpChannelListener1.HttpContextReceivedAsyncResult
1。ProcessHttpContextAsync()
關於這個例外,我發現問題是在客戶端身份驗證證書的鏈驗證,但我不知道爲什麼。 在這一點上,我卡住了! 我不知道我的證書有什麼問題,我不知道如何找到解決方案。
我真的希望有人能幫我解決這個問題。
編輯: 我們已經與另一客戶端證書測試證書鏈是在服務器和客戶端證書一樣,它不會改變任何東西。
感謝您自己分享答案。我可以問你一個問題嗎? WSDL有非對稱綁定元素? –