【问题标题】:WCF Custom Authentication ProviderWCF 自定义身份验证提供程序
【发布时间】:2010-11-08 17:27:37
【问题描述】:

我正在(拼命地)尝试让客户身份验证提供商为我的 WCF 服务工作。到目前为止,我有以下代码;

web.config

      <serviceCredentials>
        <userNameAuthentication userNamePasswordValidationMode="Custom"  customUserNamePasswordValidatorType="MyNamespace.CustomUserNameValidator, MyNamespace" />
      </serviceCredentials>

     <wsHttpBinding>
    <binding name="wsHttpBindingConfig" >
      <security mode="Message">
        <message clientCredentialType="UserName" />
      </security>
    </binding>
  </wsHttpBinding>

      <serviceBehaviors>
    <behavior>
      <serviceMetadata httpGetEnabled="true"/>
      <serviceDebug includeExceptionDetailInFaults="true"/>
    </behavior>
  </serviceBehaviors>

自定义身份验证器类;

public class CustomUserNameValidator : UserNamePasswordValidator
{
    public override void Validate(string userName, string password)
    {
        // have security details been provided?
        if (null == userName || null == password)
        {
            throw new ArgumentNullException();
        }

        // authenticate user
        if (!(userName == "test" && password == "test"))
        {
            // This throws an informative fault to the client.
            throw new FaultException("SecurityFailed");
        }
    }
}

一切编译正常,但是当我使用 Visual Studio 中的 WCF 测试客户端调用称为 Ping 的方法(如下)时,自定义身份验证器永远不会被使用。 Ping 方法和我在 CustomUserNameValidator 类中的任何断点都会执行。

为什么会这样?感谢所有帮助。

【问题讨论】:

    标签: .net wcf wcf-security wshttpbinding


    【解决方案1】:

    在你拥有的那一行

    customUserNamePasswordValidatorType="MyNamespace.CustomUserNameValidator, MyNamespace"
    

    类型的第二部分(您当前有“MyNamespace”)应该是包含该类型的程序集的名称,没有任何文件扩展名。

    请参阅this question 以获得更多帮助。

    【讨论】:

    • 在我的情况下也是一样的。 MyNamespace 实际上是 MyCompany.AplicationName.B2B 所以程序集称为 MyCompany.AplicationName.B2B.dll
    【解决方案2】:

    一些建议。

    1. WCFTestClient 不是最好的应用程序。最好使用 SOAPUI(可在 www.soapUI.org 下载)之类的工具。 WCFTestClient 不会导入所有配置设置,也不是最好的测试方法。
    2. 当我使用 CustomAuthentication 时,我的绑定设置如下:

      <wsHttpBinding>  
          <binding name="wsHttpBindingConfig" >  
             <security mode="TransportWithMessageCredentials">  
               <message clientCredentialType="UserName" />  
             </security>  
          </binding>  
      </wsHttpBinding> 
      

    我假设您会喜欢某种形式的安全性。在您的机器上使用自签名 SSL 证书进行测试非常容易。有更多关于如何做到这一点的信息here

    【讨论】:

    • Matt - TransportWithMessageCredentials 是我以前使用的,所以无论如何我都会恢复使用它。 SoapUi 也是我正在使用的一个工具——但我有时觉得它有点儿暴躁。即使使用 SoapUi,我也无法让它使用自定义身份验证。有什么建议么?另外-您说“我假设您会喜欢某种形式的安全性”。证书放在一边。这就是我想要实现的。我收到的 SOAP 消息包括带有用户名和密码的 WS-Security 标头信息,我正在尝试验证这一点。
    • 您能否提供 web.config 中的完整行为、端点和客户端(如果您正在使用)部分?
    【解决方案3】:

    我已获取您提供的所有信息并创建了模板 web.config。我会设想它看起来像这样。

    <system.serviceModel>
       <services>
         <service name="<YourNameSpace>.<ServiceName>" <behaviorConfiguration="<YourNameSpace>.<BehaviorName>">
          <endpoint
            address=""
            binding="wsHttpBinding"
            bindingConfiguration="wsHttpBindingConfig"
            contract="<YourNameSpace>.<ServiceInterface>"
          />
             <!--Notice the binding is mexHttpsBinding, default is http-->
          <endpoint 
            address="mex" 
            binding="mexHttpsBinding" 
            contract="IMetadataExchange" 
          />
         </service>
       </services>
    <behaviors>
       <serviceBehaviors>
           <behavior name="<YourNameSpace>.<BehaviorName>">
              <!--Notice the httpsGetEnabled, default is http-->
          <serviceMetadata httpsGetEnabled="true"/>
               <serviceDebug includeExceptionDetailInFaults="false"/>
               <serviceCredentials>
                  <userNameAuthentication
                     userNamePasswordValidationMode="Custom"                 
                     customUserNamePasswordValidatorType="<YourNameSpace>.CustomUserNameValidator, <YourNameSpace>"
                  />
               </serviceCredentials>
           </behavior>
       </serviceBehaviors>
    </behaviors>
    <bindings>
      <wsHttpBinding>
        <binding name="wsHttpBindingConfig">
          <security mode="TransportWithMessageCredential">
            <message clientCredentialType="UserName"/>
          </security>
        </binding>
      <wsHttpBinding>
    </bindings>
    

    【讨论】:

      猜你喜欢
      • 2012-04-05
      • 2012-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-15
      • 2020-07-23
      • 2017-11-21
      • 2011-02-09
      相关资源
      最近更新 更多