【问题标题】:Self-Hosted Wcf Service with Certificate client authentication具有证书客户端身份验证的自托管 Wcf 服务
【发布时间】:2019-05-26 11:06:02
【问题描述】:

我有一个自托管的 wcf 服务,它使用带有传输安全性的 wsHttpBinding。我希望该服务使用证书对客户端进行身份验证。

当客户端使用设置为“无”的 clientCredentialsType 与服务通信时,一切正常。 该证书是使用 OpenSSL(自签名)创建的,并在特定端口上使用 netsh 注册。证书的名称是 DomainName(在我的例子中是机器名称)。

更新
我已经为客户端和服务器创建了证书,并将它们放置在彼此的受信任根存储中(客户端存储中的服务器证书,反之亦然)。

我在 SOF 浏览了很多文章和其他问题,但即使是看起来相关的文章也无法帮助我解决问题。

当前配置

服务

<system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="serviceBehavior">
          <serviceMetadata httpsGetEnabled="true" httpGetEnabled="false" />
          <serviceDebug includeExceptionDetailInFaults="true" httpsHelpPageEnabled="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <bindings>
      <wsHttpBinding>
        <binding name="httpsBinding" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" sendTimeout="01:00:00">
          <security mode="Transport" >
            <transport clientCredentialType="Certificate"/>
          </security>
        </binding>
      </wsHttpBinding>
    </bindings>
    <services>
      <service behaviorConfiguration="serviceBehavior" name="Service">
        <endpoint binding="wsHttpBinding" bindingConfiguration="httpsBinding" contract="SomeNamespace.IService"/>
        <host>
          <baseAddresses>
            <add baseAddress="https://domain:port/Something/"/>
          </baseAddresses>
        </host>
      </service>
    </services>    
  </system.serviceModel>

客户

<system.serviceModel>  
    <client>  
      <endpoint address="https://domain:port/Something/"   
                behaviorConfiguration="endpointCredentialBehavior"  
                binding="wsHttpBinding"   
                bindingConfiguration="Binding"   
                contract="SomeNamespace.IService"/>  
    </client>  
    <behaviors>  
      <endpointBehaviors>  
        <behavior name="endpointCredentialBehavior">  
          <clientCredentials>  
            <clientCertificate findValue="DomainName"  
                               storeLocation="LocalMachine"  
                               storeName="My"  
                               x509FindType="FindBySubjectName" />  
          </clientCredentials>  
        </behavior>  
      </endpointBehaviors>  
    </behaviors>  
    <bindings>  
      <wsHttpBinding>         
        <binding name="Binding">  
          <security mode="Transport">  
            <transport clientCredentialType="Certificate"/>  
          </security>  
        </binding>  
      </wsHttpBinding>  
    </bindings>  
  </system.serviceModel>  

我得到以下异常:

System.ServiceModel.Security.MessageSecurityException:HTTP 请求被客户端身份验证方案“匿名”禁止。 ---> System.Net.WebException:远程服务器返回错误:(403)禁止。 在 System.Net.HttpWebRequest.GetResponse() 在 System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan 超时)

【问题讨论】:

    标签: .net wcf wcf-binding wcf-security wcf-client


    【解决方案1】:

    我们还需要做一件事。我们应该使他们的证书相互信任。服务器的证书必须被客户端信任,客户端的证书必须被服务器信任。
    具体来说,服务器证书安装在客户端 Trusted Root Certificate Authorities 中,客户端证书安装在服务器 Trusted Root Certificate Authorities 中。最好使用本地机器证书存储。我们可以使用 Certlm.msc 命令检查证书。
    关于这个问题请参考官方文档。
    https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/transport-security-with-certificate-authentication
    对于其他特殊工具创建的一些特殊证书,如PowerShell,我们最好将项目的目标框架(服务器和客户端)设为4.6.2以上。
    https://github.com/dotnet/docs/issues/12000
    如果有什么我可以帮忙的,请随时告诉我。

    【讨论】:

    • 感谢您的回复。我已经按照您的建议进行了操作,但仍然无法正常工作(我已使用此信息更新了问题)。我也浏览了你提到我的文章,但仍然无济于事。您还有什么可以建议的吗?
    • 在我让应用程序正常工作之前,我也遇到了这个异常。是否将Everyone账户添加到证书私钥的管理组?此外,为什么您的客户端配置文件中没有 Identity 部分?如果通过添加服务引用来调用服务,配置文件会自动添加身份部分,如下所示。
    • 该值为服务器证书的公钥或服务器的机器名。
    • 您能指出我将Everyone 帐户添加到管理组的位置吗?
    • 我们的项目有一个共同的客户端基础设施,因为我们有多个不同的客户端连接到同一个服务器,并且我们使用 ClientBase 类手动发送我们的请求。我将检查如何手动添加身份部分。
    【解决方案2】:

    您的配置文件看起来不错。 我的 3rd 方客户端证书(公钥)有同样的问题,无论我在哪里添加该证书仍然不起作用。 但是,我设法使它与我的自签名客户端证书一起使用。我只需要将客户端 CA 证书添加到服务器上的受信任的根证书颁发机构。

    我发现这两个主题有助于确定一些问题: Mutual authentication with a IIS hosted WCF WCF – 2 Way SSL Security using Certificates

    希望对您有所帮助。如果我能解决这个问题,我会发布更新。

    【讨论】:

    • 我已经解决了第 3 方客户端证书(公钥)的问题,问题是由于测试我没有该证书的私钥,所以我的服务器无法验证客户端。我还设法在 IIS 中正确启用证书映射(使用 Netsh 命令启用 DS 映射)并限制所有未使用证书进行映射的用户的访问权限。我在 webconfig 中添加了授权,而不是在 IIS 中禁用匿名访问:
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-09
    • 2013-10-07
    • 2012-01-03
    • 1970-01-01
    • 2018-02-12
    • 1970-01-01
    • 2014-05-21
    相关资源
    最近更新 更多