哪个Membership Provider实现了存储在web.config中的用户?

tom*_*ing 10 asp.net membership-provider

有一个代码盲目的时刻.

ASP.NET 4.0.

Web.config文件:

<?xml version="1.0"?>
<configuration>
  <system.web>
    <authentication mode="Forms">
      <forms name="DataViewer" loginUrl="login.aspx">
        <credentials passwordFormat="Clear">
          <user name="devuser" password="test" />
        </credentials>
      </forms>
    </authentication>
    <authorization>
      <deny users="?" />
    </authorization>
  </system.web>
Run Code Online (Sandbox Code Playgroud)

和登录控制:

  <asp:Login ID="login" runat="server" />
Run Code Online (Sandbox Code Playgroud)

如果我输入用户名和密码,然后单击"登录",则会挂起.

如果我中断,我可以在调用堆栈中看到login.AuthenticateUsingMembershipProvider()正在调用SqlMembershipProvider.ValidateUser().根本没有定义或涉及此项目的数据库,我没有指定应该使用SqlMembershipProvider.

所以我的问题是,我应该使用什么成员资格提供程序来使ASP.NET使用<credentials>元素中的用户名和密码web.config

tom*_*ing 15

考虑到框架设计人员如何定义一个<credentials />他们没有实现任何代码来消耗它的元素,我感到很惊讶.

我在这里找到了一个类似的工作实现,我已修复并包含在下面.MembershipProvider投掷的所有其他成员NotImplementedException.

using System.Configuration;
using System.Web.Configuration;
using System.Web.Security;

public class WebConfigMembershipProvider : MembershipProvider
{
    private FormsAuthenticationUserCollection _users = null;
    private FormsAuthPasswordFormat _passwordFormat;

    public override void Initialize(string name,
      System.Collections.Specialized.NameValueCollection config)
    {
        base.Initialize(name, config);
        _passwordFormat = getPasswordFormat();
    }

    public override bool ValidateUser(string username, string password)
    {
        var user = getUsers()[username];
        if (user == null) return false;

        if (_passwordFormat == FormsAuthPasswordFormat.Clear)
        {
            if (user.Password == password)
            {
                return true;
            }
        }
        else
        {
            if (user.Password == FormsAuthentication.HashPasswordForStoringInConfigFile(password,
                _passwordFormat.ToString()))
            {
                return true;
            }
        }

        return false;
    }

    protected FormsAuthenticationUserCollection getUsers()
    {
        if (_users == null)
        {
            AuthenticationSection section = getAuthenticationSection();
            FormsAuthenticationCredentials creds = section.Forms.Credentials;
            _users = section.Forms.Credentials.Users;
        }
        return _users;
    }

    protected AuthenticationSection getAuthenticationSection()
    {
        Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
        return (AuthenticationSection)config.GetSection("system.web/authentication");
    }

    protected FormsAuthPasswordFormat getPasswordFormat()
    {
        return getAuthenticationSection().Forms.Credentials.PasswordFormat;
    }
}
Run Code Online (Sandbox Code Playgroud)