【问题标题】:WCF Certificate Based Authentication Issue. Service is Getting authenticated with an invalid Certificate基于 WCF 证书的身份验证问题。服务正在使用无效证书进行身份验证
【发布时间】:2021-06-21 13:43:54
【问题描述】:

我是 WCF 服务的初学者。尝试在 WCF 服务上实施基于证书的身份验证并面临问题。该服务需要来自调用客户端的特定证书。如果客户端未通过任何证书,服务器将引发身份验证错误。但同时,服务调用正在通过客户端提供的任何证书进行身份验证(如果客户端提供特定证书,服务假设进行身份验证)。

以下是服务器配置的代码sn-p:

服务配置:

<bindings>
    <wsHttpBinding>
        <binding name="MyWsHttpBinding" maxReceivedMessageSize="2147483647" receiveTimeout="00:30:00">
            <readerQuotas maxStringContentLength="2147483647" maxBytesPerRead="2147483647" maxDepth="2147483647" maxArrayLength="2147483647"/>
            <security mode="Transport">
                <transport clientCredentialType="None" proxyCredentialType="None"/>
                <message clientCredentialType="Certificate" algorithmSuite="Default"/>
            </security>
        </binding>
    </wsHttpBinding>
</bindings>

<serviceBehaviors>
    <behavior name="MyServiceBehavior">
        <serviceCredentials>
            <clientCertificate>
                <authentication certificateValidationMode="ChainTrust" />
            </clientCertificate>
            <serviceCertificate findValue="e616ebcd940951794736624acc6484802018c8d4" storeLocation="LocalMachine" storeName="My" x509FindType="FindByThumbprint" />
        </serviceCredentials>
        <serviceMetadata httpsGetEnabled="true" httpGetEnabled="true"/>
        <CustomBehaviorExtensionElement/>
        <serviceDebug includeExceptionDetailInFaults="true"/>
    </behavior>
</serviceBehaviors>


<endpointBehaviors>
    <behavior name="MyEndpointBehavior">
        <MySchemaValidator validateRequest="True" validateReply="False">
            <schemas>
                <add location="App_Data\model-service.xsd"/>
            </schemas>
        </MySchemaValidator>
    </behavior>
</endpointBehaviors>


<services>
    <service name="MyService" behaviorConfiguration="MyServiceBehavior">
        <endpoint binding="wsHttpBinding" bindingConfiguration="MyWsHttpBinding" contract="MyExchangeService" behaviorConfiguration="MyEndpointBehavior" bindingNamespace="http://www.mycompany.com/exchange/"/>
        <endpoint contract="IMetadataExchange" binding="mexHttpsBinding" address="mex" name="mex"/>
    </service>
</services>

【问题讨论】:

    标签: c# web-services wcf authentication soap


    【解决方案1】:

    问题的原因是您使用的安全模式是传输,所以只有以下代码有效:

      <transport clientCredentialType="None" proxyCredentialType="None"/>
    

    以下消息设置无效:

     <message clientCredentialType="Certificate" algorithmSuite="Default"/>
    

    将transport中的值改为certificate,也可以在官网下载wcf demo,有相关证书验证的例子,还有tutorials corresponding to the demo

    【讨论】:

    • 将配置更改为:&lt;transport clientCredentialType="Certificate" proxyCredentialType="None"/&gt;。修改后 IIS 抛出以下错误:服务“SslRequireCert”的 SSL 设置与 IIS“无”的设置不匹配。
    【解决方案2】:

    我看到你的代码中使用的证书验证模式是ChainTrust

    <clientCertificate>
        <authentication certificateValidationMode="ChainTrust" />
    </clientCertificate>
    

    正如Microsoft Docs 中提到的,使用ChainTrust 意味着-

    如果链构建到受信任根存储中的证书颁发机构,则证书有效

    意思是,客户端不需要发送与您的服务 web.config 中提到的完全相同的指纹的证书。
    事实上,任何其根/中间证书颁发机构存在于 VM 的 Trusted Root Store 中的证书都将通过验证。

    为确保客户端能够仅使用特定证书对您的服务进行身份验证,请将 ChainTrust 更改为 PeerTrust 并将证书添加到受信任的人存储在您 VM 的证书存储 (certmgr) 上。

    <authentication certificateValidationMode="PeerTrust" />
    

    参考资料:

    1. MS Docs - Working with certificates in WCF
    2. Authentication element in web.config
    3. More info on Certificate Chain of Trust

    【讨论】:

      猜你喜欢
      • 2018-02-24
      • 1970-01-01
      • 1970-01-01
      • 2023-03-29
      • 2016-10-02
      • 1970-01-01
      • 2011-10-05
      • 2011-11-03
      • 2013-07-05
      相关资源
      最近更新 更多