Wcf自定义证书验证不会验证

时间:2015-03-24 12:46:54

标签: c# .net wcf certificate x509certificate

我试图在我的wcf配置中找到错误,但我无法找到问题所在。我需要更多的眼睛;无论如何,我想在wcf中使用自定义认证验证。我在我的CertificateValidator类中设置了断点,但是这个断点并没有捕获任何请求,但是应用程序正在运行,我可以发送请求(没有证书)。

这是我的配置

<system.serviceModel>
  <bindings>
    <basicHttpBinding>
      <binding maxReceivedMessageSize="104857600">
        <security>
          <message clientCredentialType="Certificate" />
        </security>
      </binding>
    </basicHttpBinding>
  </bindings>

  <services>
    <service name="AccountService" behaviorConfiguration="">
      <endpoint address="" binding="basicHttpBinding" bindingConfiguration="" contract="IAccountService"></endpoint> 
    </service>
    <service name="PortalService" behaviorConfiguration="">
      <endpoint address="" binding="basicHttpBinding" bindingConfiguration="" contract="IPortalService"></endpoint>
    </service>
  </services>

  <behaviors>
    <serviceBehaviors>
      <behavior>
        <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
        <serviceDebug includeExceptionDetailInFaults="false"/>
        <serviceCredentials>
          <clientCertificate>
            <authentication certificateValidationMode="Custom" customCertificateValidatorType="Services.Validators.CertificateValidator, Services" />
          </clientCertificate>
        </serviceCredentials>
      </behavior>
    </serviceBehaviors>
  </behaviors>
  <protocolMapping>
      <add binding="basicHttpsBinding" scheme="https" />
  </protocolMapping>    
  <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>

1 个答案:

答案 0 :(得分:0)

您使用的是NET 4还是4.5?

我认为你错过的事情:

  • 您的自定义basicHttpBinding需要使用<security mode="Message">(目前您没有指定它,这意味着默认为"None"
  • 您的自定义basicHttpBinding需要name=,因此可以引用
  • 您的终端需要使用basicHttpBinding
  • 引用自定义bindingConfiguration=
  • 您的自定义服务行为需要name=,因此可以引用
  • 您的服务需要使用behaviorConfiguration=
  • 来引用自定义服务行为
  • 您的服务需要在<serviceCertificate>中指定<serviceCredentials>,该协议用于与客户建立安全通道
  • 您的客户需要知道'<serviceCertificate>'中的<clientCredentials>,以便它可以与服务器建立安全通道
  • 您的客户端需要将用于凭证的证书附加到发送到服务器的请求中(通过在.config中的<clientCertificate>中指定<clientCredentials>或以编程方式)
  • 确保您已正确创建和安装证书

注意:您应该至少创建了2个证书 - 1用于服务器的标识 - 客户使用1个或多个凭证(取决于您是否决定所有客户应出示相同的证书,或者如果您为每个客户分配唯一的证书) - 请勿使用相同的证书进行服务器识别和客户端凭证

一些帮助理解“消息级安全性”的链接:

如果您的邮件交换速度太慢,请考虑使用“传输”级安全性。

试试这个:

  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="securebasicHttpBinding" maxReceivedMessageSize="104857600">
          <security mode="Message">
            <message clientCredentialType="Certificate" />
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>

    <services>
      <service behaviorConfiguration="secureBehaviour" name="AccountService">
        <endpoint address="" binding="basicHttpBinding" bindingConfiguration="securebasicHttpBinding"
          contract="IAccountService" />
      </service>
      <service behaviorConfiguration="secureBehaviour" name="PortalService">
        <endpoint address="" binding="basicHttpBinding" bindingConfiguration="securebasicHttpBinding"
          contract="IPortalService" />
      </service>
    </services>

    <behaviors>
      <serviceBehaviors>
        <behavior name="secureBehaviour">
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
          <serviceCredentials>
            <serviceCertificate ...something goes here... />
            <clientCertificate>
              <authentication certificateValidationMode="Custom" customCertificateValidatorType="Services.Validators.CertificateValidator, Services" />
            </clientCertificate>
          </serviceCredentials>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
      <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>