首页 > 解决方案 > 具有证书客户端身份验证的自托管 Wcf 服务

问题描述

我有一个自托管的 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 超时)

标签: .netwcfwcf-bindingwcf-securitywcf-client

解决方案


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

我发现这两个主题有助于确定一些问题: 使用 IIS 托管的 WCF 进行相互身份验证 WCF – 2 Way SSL Security using Certificates

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


推荐阅读