设置IIS以要求客户端证书并使用匿名身份validation

设置IIS以要求客户端证书并使用匿名身份validation

我有一个WCF Web服务供我们的客户使用。 我想要使​​用客户端证书来保护这一点。 我也将使用客户证书来识别客户。

我已经使识别部分工作,但我不能让IIS要求客户端证书。

如果我将IIS设置为接受客户端证书,则通信将起作用,并且可以使用以下命令获取客户端标识:

ServiceSecurityContext.Current.PrimaryIdentity.Name 

但是我也可以访问没有客户端证书的网站。 我不确定没有其他人可以做什么,而不是阅读WSDL,但我不希望没有可信任证书的人能够获得任何信息。

如果我将IIS设置为需要客户端证书,那么应该有权访问的testing客户端会收到错误消息:

客户端身份validationscheme“匿名”禁止HTTP请求。

我想只允许那些有服务器信任的客户端证书的用户访问。 其他人将被拒绝。

服务器WCFconfiguration:

 <system.serviceModel> <behaviors> <serviceBehaviors> <behavior name="DefaultBehavior"> <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" /> <serviceDebug includeExceptionDetailInFaults="true" /> <serviceCredentials> <clientCertificate> <authentication certificateValidationMode="ChainTrust" /> </clientCertificate> <serviceCertificate findValue="64343ee2c8338518e78ba698f3936dc92c90db57" x509FindType="FindByThumbprint" /> </serviceCredentials> </behavior> </serviceBehaviors> </behaviors> <bindings> <wsHttpBinding> <binding name="DefaultBinding"> <security mode="TransportWithMessageCredential"> <transport clientCredentialType="Certificate" /> <message clientCredentialType="Certificate" /> </security> </binding> </wsHttpBinding> </bindings> <services> <service name="WebService.Service" behaviorConfiguration="DefaultBehavior"> <endpoint address="" binding="wsHttpBinding" bindingConfiguration="DefaultBinding" contract="WebService.IService" /> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> </service> </services> </system.serviceModel> 

客户端WCFconfiguration。

 <system.serviceModel> <behaviors> <endpointBehaviors> <behavior name="DefaultBehavior"> <clientCredentials> <clientCertificate storeLocation="LocalMachine" findValue="d084c91a8f81878cd4dd991bbab348235f0fd1a3" x509FindType="FindByThumbprint" /> <serviceCertificate> <authentication certificateValidationMode="ChainTrust" /> </serviceCertificate> </clientCredentials> </behavior> </endpointBehaviors> </behaviors> <bindings> <wsHttpBinding> <binding name="WSHttpBinding_IService"> <security mode="TransportWithMessageCredential"> <transport clientCredentialType="Certificate" /> <message clientCredentialType="Certificate" /> </security> </binding> </wsHttpBinding> </bindings> <client> <endpoint address="https://host/WebService/Service.svc" behaviorConfiguration="DefaultBehavior" binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IService" contract="WebService.IService" name="WSHttpBinding_IService"> </endpoint> </client> </system.serviceModel> 

好的,我们也和你一样。 我们反过来工作。 我们首先使用客户端和服务器证书来保护IIS。 我们在IIS Express上完成了这个工作(当我发布这个时,仍在开发中)。 我们允许在IIS express applicationhost.config中覆盖web.config的特定部分。 即: <section name="windowsAuthentication" overrideModeDefault="Allow" />

服务器端configuration:

 <sytem.serviceModel> <bindings> <wsHttpBinding> <binding name="ClientCert"> <security mode="Transport"> <transport clientCredentialType="Certificate" /> </security> </binding> </wsHttpBinding> </bindings> <behaviors> <!--We have a custom service behavior for claim based security --> <behavior name="wsHttpCertificateBehavior"> <serviceMetadata httpGetEnabled="false" httpsGetEnabled="true" /> <serviceDebug httpHelpPageEnabled="true" includeExceptionDetailInFaults="true" /> <serviceAuthorization serviceAuthorizationManagerType="MyNamespace.AdamAuthorizationManager, MyAssembly"> <authorizationPolicies> <add policyType="MyNamespace.AdamAuthorizationPolicy, MyAssembly" /> </authorizationPolicies> </serviceAuthorization> <serviceCredentials> <serviceCertificate findValue="YourServerCertificateNameWithoutCN=" x509FindType="FindBySubjectName" storeLocation="LocalMachine" storeName="My" /> <clientCertificate> <authentication revocationMode="NoCheck" mapClientCertificateToWindowsAccount="true" /> </clientCertificate> </serviceCredentials> </behavior> </behaviors> <services> <service name="MyNamespace.OrderService" behaviorConfiguration="wsHttpCertificateBehavior"> <endpoint address="https://iisurl/OrderService.svc/ClientCert" contract="wsHttpCertificateBehavior.IOrderService" binding="wsHttpBinding" bindingConfiguration="ClientCert"> </endpoint> </service> </services> </sytem.serviceModel> <system.webServer> <security> <authentication> <windowsAuthentication enabled="true" /> <anonymousAuthentication enabled="true" /> <iisClientCertificateMappingAuthentication defaultLogonDomain="YourDomain" enabled="true" oneToOneCertificateMappingsEnabled="true"> <oneToOneMappings> <add enabled="true" certificate="Base64HashOfTheCertificate" userName="YourUserName" password="YourPassword" /> </oneToOneMappings> </iisClientCertificateMappingAuthentication> </authentication> <authorization> <add users="*" accessType="Allow" /> </authorization> <!--Require SSL *AND* require a client certificate --> <access sslFlags="Ssl, SslNegotiateCert, SslRequireCert" /> </security> </system.WebServer> 

在客户端:

 <system.serviceModel> <wsHttpBinding> <binding name="ClientCertificate"> <security mode="Transport"> <transport clientCredentialType="Certificate"/> </security> </binding> </wsHttpBinding> <behaviors> <endpointBehaviors> <behavior name="wsHttpCertificateBehavior"> <clientCredentials> <clientCertificate findValue="YourClientCertificateNameWithoutCN=" storeLocation="CurrentUser" storeName="My" x509FindType="FindBySubjectName"/> <serviceCertificate> <authentication revocationMode="NoCheck"/> </serviceCertificate> </clientCredentials> </behavior> </endpointBehaviors> </behaviors> <client> <endpoint name="ClientCertificate" address="https://iisurl/OrderService.svc/ClientCert" contract="MyNamespace.IOrderService" binding="wsHttpBinding" bindingConfiguration="ClientCertificate" behaviorConfiguration="wsHttpCertificateBehavior"> </endpoint> </client> </system.serviceModel> 

启用跟踪,login服务以及自定义授权策略和IIS跟踪日志对我们有很大的帮助。

我们的iisurl映射到我们的主机文件127.0.0.1,所以我们有可信的证书。 对于iisClientCertificationMapping检查了这一点 。

不知道你的SSL设置是否正确。 我们有一个PowerShell脚本。 它的一些部分:

生成根证书(PowerShell)

 Invoke-Command -ScriptBlock{ . "C:\Program Files\Microsoft SDKs\Windows\v7.0A\bin\makecert.exe" -n "CN=YourRootCA" -r -sv YourRootCA.pvk YourRootCA.cer} $certFile = get-childitem $exPath | where {$_.FullName -match "GlobalVisionServicesRootCA.cer"} if ($certFile -ne $NULL) { echo "Discovered the YourRootCA.cer in the same folder as this script, installing it in the LocalMachine\Root certificate store.." $cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2($certFile.FullName) $rootStore = new-object system.security.cryptography.x509certificates.x509Store 'Root','LocalMachine' $rootStore.Open('ReadWrite') $rootStore.Add($cert) $rootStore.Close() } 

生成服务器证书(命令行):

 makecert.exe -sk YourDevSrvCert -iv YourRootCA.pvk -n "CN=iisurl" -ic YourRootCA.cer -sr localmachine -ss my -sky exchange -pe yourservercertificate.cer 

生成服务器客户端(命令行):

 makecert.exe -sk ClientDevSrvCert -iv YourRootCA.pvk -n "CN=iisurl" -ic GlobalVisionServicesRootCA.cer -sr localmachine -ss my -sky exchange -pe iisurl.cer 

将证书绑定到IIS(命令行,特定于XP):

 httpcfg.exe delete ssl -i "0.0.0.0:443" httpcfg.exe" delete urlacl url="https://iisurl:443/" httpcfg.exe set urlacl url="https://iisurl:443/" user=Everyone httpcfg.exe" set ssl -i "0.0.0.0:443" -h ThumpPrint 

将ThumpPrint更改为主题名为iisurl的证书的ThumpPrint。 我build议你完全自动化与PowerShell,我们有这个,所以我们可以在多台机器上开发。 但是我不能把它全部放在这里。

我希望这可以帮助你。 有了这个configuration,如果你通过https浏览url iisurl / OrderService它要求你提供一个客户端证书。 (在IE中)

你也可以看这个日志:C:\ WINDOWS \ system32 \ Logfiles \ HTTPERR