具有 basicHttpBinding、传输安全和基本客户端凭据类型的自定义 WCF 凭据验证器

Custom WCF credential validator with basicHttpBinding, Transport security, and Basic client credential type

我一直在阅读 WCF 配置文档并希望确认我遇到的限制。

我有一个客户端需要以下绑定配置:

<bindings>
  <basicHttpBinding>
    <binding name="secureHttpBinding">
      <security mode="Transport">
        <transport clientCredentialType="Basic"/>
      </security>
    </binding>
  </basicHttpBinding>
</bindings>

在服务器端,我实现了自定义用户名和密码验证器,因为我们不为客户端使用 Windows 或域帐户。我遵循了本指南:https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/how-to-use-a-custom-user-name-and-password-validator,但他们和我看到的其他示例使用 wsHttpBinding 或添加消息安全性。

服务器端点和行为如下所示:

<services>
  <service name="MyWcfService.Service" behaviorConfiguration="MyBehavior">
    <endpoint
      address=""
      binding="basicHttpBinding"
      bindingConfiguration="secureHttpBinding"
      contract="MyWcfService.IService"/>
    <endpoint
      address="mex"
      binding="mexHttpsBinding"
      contract="IMetadataExchange"/>
  </service>
</services>

<behaviors>
  <serviceBehaviors>
    <behavior name="MyBehavior">
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
      <serviceCredentials>
        <userNameAuthentication
          userNamePasswordValidationMode="Custom" 
          customUserNamePasswordValidatorType="MyValidator, MyWcfService"/>
      </serviceCredentials>
    </behavior>
  </serviceBehaviors>
</behaviors>

在启用基本身份验证的 IIS 中托管服务时,未命中自定义验证器,IIS 尝试针对 Windows 帐户验证提供的凭据,如果用户帐户不存在,则会失败并显示此消息:

The HTTP request is unauthorized with client authentication scheme 'Basic'.

那么,是否不可能为 IIS 中托管的具有 basicHttpBinding、传输安全模式和基本传输客户端凭据类型的服务提供自定义凭据验证程序?没有办法覆盖 IIS 执行的基本 Windows 身份验证吗?

我们应该使用 UserName 身份验证而不是基本身份验证,随后,我们可以为该服务提供自定义凭据验证器。

  <basicHttpBinding>
     <binding name="mybinding">
       <security mode="TransportWithMessageCredential">
         <message clientCredentialType="UserName"/>
       </security>
     </binding>
   </basicHttpBinding>

 <serviceCredentials>
        <userNameAuthentication customUserNamePasswordValidatorType="WcfService3.CustUserNamePasswordVal,WcfService3" userNamePasswordValidationMode="Custom"/>
      </serviceCredentials>
    </behavior>
  </serviceBehaviors>
</behaviors>

详情,
How to add user/pass authentication in WCF
如果有什么我可以帮忙的,请随时告诉我。

到目前为止,似乎无法通过此绑定配置使用自定义凭据验证器。

<basicHttpBinding>
<binding name="secureHttpBinding">
  <security mode="Transport">
    <transport clientCredentialType="Basic"/>
  </security>
</binding>