2

我已经构建了一个 WCF helloworld 客户端和服务器。我想在它们之间使用证书身份验证。

我得到的错误是“调用者未经服务验证”。

我使用 makecert.exe 创建了两个证书。客户端上的证书安装在“个人”和“受信任的人”和“第三方根证书颁发机构”下。我复制了证书,因为我不知道它是否应该只在一个标题下

我的服务器 webconfig 如下

            <?xml version="1.0"?>
            <configuration>
              <appSettings>
                <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true"/>
              </appSettings>
              <system.web>
                <compilation debug="false" targetFramework="4.5.1"/>
                <httpRuntime targetFramework="4.5.1"/>
              </system.web>
              <system.serviceModel>
                <services>
                  <service behaviorConfiguration="BusinessToBusiness" name="TestHelloWork.Service1">

                    <endpoint address="" binding="wsHttpBinding"  bindingConfiguration="BindingConfig" contract="TestHelloWork.IService1" /> 
                            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
                    <host>
                      <baseAddresses>
                        <add baseAddress="http://win-gat-web01:7777/Service1"/>
                      </baseAddresses>
                    </host>
                  </service>
                </services>
                <bindings>  
                  <wsHttpBinding>  
                    <binding name="BindingConfig">
                     <security>
                        <message clientCredentialType = "Certificate"/>
                     </security>
                    </binding>  
                  </wsHttpBinding>  
               </bindings>  
                <behaviors>
                  <endpointBehaviors>
                    <behavior name="webBehavior">
                      <webHttp/>
                    </behavior>
                  </endpointBehaviors>
                  <serviceBehaviors>
                    <behavior>
                      <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
                      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
                      <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
                      <serviceDebug includeExceptionDetailInFaults="true"/>
                    </behavior>
                    <behavior name="BusinessToBusiness">
                     <serviceCredentials>
                        <clientCertificate>
                           <authentication certificateValidationMode = "PeerTrust"/>
                        </clientCertificate>
                        <serviceCertificate findValue="WCfServer"
                storeLocation="LocalMachine"
                storeName="My"
                x509FindType="FindBySubjectName" />         
                     </serviceCredentials>
                     <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
                    </behavior>     
                  </serviceBehaviors>
                </behaviors>
                <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
              </system.serviceModel>
            </configuration>

客户端web.config如下

            <?xml version="1.0"?>

            <!--
              For more information on how to configure your ASP.NET application, please visit
              http://go.microsoft.com/fwlink/?LinkId=169433
              -->

            <configuration>

                <system.web>
                  <compilation debug="true" targetFramework="4.5.1" />
                  <httpRuntime targetFramework="4.5.1" />
                </system.web>
              <system.serviceModel>
            <bindings>
              <wsHttpBinding>
                <binding name="WSHttpBinding_IService1">
                  <security>
                    <message clientCredentialType="Certificate" />

                  </security>
                </binding>
              </wsHttpBinding>
            </bindings>
            <client>
              <endpoint address="http://myserver:7777/Service.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>

客户端证书安装在“个人”下

知道有什么问题吗?我用谷歌搜索并意识到他们应该在同一个域上?但他们是。此外,当我的服务是外部服务时,域将如何工作?

4

2 回答 2

3

尝试启用 CAPI2 日志。它是包含证书验证信息的特殊日志(默认情况下未启用)。如果您的问题在于证书验证程序失败,您将在那里找到有用的信息。寻找错误。就我而言,它类似于

  • 已处理的证书链,但在信任提供者不信任的根证书中终止。
  • 无法将证书链构建到受信任的根颁发机构。
  • 吊销功能无法检查吊销...

CAPI2 日志

在我的情况下,我有

  • Client.pfx 导入客户端上的 CurrentUser\My(个人)存储。
  • 颁发 client.pfx 的 CA 证书 (ClientCA) 被导入到客户端上的 LocalMachine 存储和服务器上的 LocalMachine 存储的受信任的根权威机构。
  • server.pfx 导入 LocalMachine\My(个人)存储在服务器上
  • 颁发 server.pfx 的 CA 证书 (ServerCA) 导入到客户端上的 LocalMachine 存储和服务器上的 LocalMachine 存储中的受信任的根权限。
  • 客户端和服务器均可使用 CRL 到客户端 CA
  • 客户端和服务器均可使用 CRL 到服务器 CA

但是您正在使用PeerTrust证书验证模式,所以根据 WCF 演示的这个评论,我有

将 certificateValidationMode 设置为 PeerOrChainTrust 意味着如果证书在用户的 Trusted People 存储中,那么它将被信任,而无需对证书的颁发者链进行验证。此处使用此设置是为了方便,以便可以运行示例而无需证书颁发机构 (CA) 颁发的证书。

我认为证书应该像这样放置:

  • client.cer(仅来自 client.pfx 的证书)导入到受信任的人员存储在服务器上的 LocalMachine 存储中
  • server.cer(仅来自 client.pfx 的证书)导入到客户端上 CurrentUser 存储中的受信任人员存储

验证您是否已将服务器私钥的权限授予运行 IIS WCF 服务的 AppPool(默认池为IIS APPPOOL\DefaultAppPool) 可以使用mmccertlm.msc通过右键单击服务器证书然后All Tasks->来完成Manage Private Keys ...。确认您没有选择 AD,因为 IIS APPPOOL 是本地组。添加帐户IIS APPPOOL\your_pool_name并点击确定。

如果您在 AppPool 中有默认设置,例如Identity设置为AplicationPoolIdentity而不是自定义帐户(通常使用 AD 中的托管服务帐户)并且Load User Profile设置为true.

为服务器私钥添加权限 默认应用程序池设置

于 2017-11-20T13:00:42.607 回答
0

您遇到的 WCF 证书身份验证问题很可能与使用 MakeCert 生成自签名证书时使用的选项有关。

特别是,确保您的证书支持必要的选项/目的。(例如,证书的预期用途字段应包含适当的值,例如“服务器身份验证”或“客户端身份验证”。)

Microsoft 的以下链接详细说明了该过程。
https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/how-to-create-temporary-certificates-for-use-during-development

注意:我们的团队很幸运地使用了SelfCert工具来快速生成开发、自签名证书。

于 2017-11-20T13:34:57.270 回答