WCF配置 - 主机上的自定义身份验证

时间:2013-07-29 14:45:10

标签: wcf web-config wcf-security

我一直在努力解决这个问题 - 我无法正确配置此WCF服务以使用自定义成员资格提供程序。当我启动测试客户端时,我收到此错误:

  

用户名/密码配置中指定的成员资格提供商MidlandsCoop.MembersWcfService.Business.UserAuthentication无效。没有发现在system.web / membership / providers下注册的此类提供商。

我一直关注这个MSDN link无济于事。谁能告诉我哪里出错了?

这是我的web.config:

<?xml version="1.0"?>
<configuration>
  <connectionStrings>
<add name="MyCS" connectionString="Data Source=my server;Initial Catalog=MyDB;Integrated Security=True" providerName="System.Data.SqlClient"/>
  </connectionStrings>
  <system.web>
<compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
<bindings>
  <wsHttpBinding>
    <binding name="wsHttpBinding">
      <security>
        <message clientCredentialType="UserName" />
      </security>
    </binding>
  </wsHttpBinding>
</bindings>
<services>
  <service name="MidlandsCoop.MembersWcfService.MembersService" behaviorConfiguration="Service_Behaviour">
    <endpoint address="" binding="basicHttpBinding" bindingConfiguration=""
      name="basicHttpBinding" contract="MidlandsCoop.MembersWcfService.IMembersService" />
    <endpoint binding="wsHttpBinding" bindingConfiguration="wsHttpBinding"
      name="wsHttpBinding" contract="MidlandsCoop.MembersWcfService.IMembersService" />
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior name="Service_Behaviour">
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="false" />
      <serviceCredentials>
        <userNameAuthentication userNamePasswordValidationMode="Custom"
          membershipProviderName="MidlandsCoop.MembersWcfService.Business.UserAuthentication" />
      </serviceCredentials>
    </behavior>
  </serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true"     aspNetCompatibilityEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
  </system.webServer>
</configuration>

我的用户身份验证类如下:

public class UserAuthentication : UserNamePasswordValidator
{
    public override void Validate(string userName, string password)
    {
        if (null == userName || null == password)
        {
            throw new ArgumentNullException();
        }

        var db = new PetaPoco.Database("MyDB");

        db.Fetch<dynamic>("SELECT Username, Password FROM MyTable WHERE Username=@0 AND Password =@1",
                             userName, password);
    }
}

任何建议都将不胜感激。

1 个答案:

答案 0 :(得分:2)

您提供的链接提到customUserNamePasswordValidatorType以指定自定义身份验证类型,在您的情况下为UserAuthentication。我认为MembershipProviderName应该是从MembershipProvider类派生的类。