哪个成员资格提供程序实现了存储在web.config中的用户?

11

脑抽了一下。

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>

还有一个登录控件:

  <asp:Login ID="login" runat="server" />

当我输入用户名和密码并单击“登录”时,它卡住了。

如果我断点调试,我可以看到调用堆栈中login.AuthenticateUsingMembershipProvider()正在调用SqlMembershipProvider.ValidateUser()。在这个项目中根本没有定义或涉及数据库,而且我也没有指定要使用 SqlMembershipProvider。

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


可能是因为默认的成员身份验证提供程序是AspNetSqlProvider,它使用SQL Server数据库作为其用户存储。 - Scorpio
我希望这只是一个临时解决方案。在web.config中维护凭据将变得非常困难,因为每次更改都会重新启动Web应用程序。 - jrummell
当然可以。但它适用于手头的场景。我需要为一个单一用户(永远不会更改密码)的简单应用程序提供简单的保护。具有更多依赖项的解决方案不合适。 - tomfanning
4个回答

16

我很惊讶,考虑到框架设计师费尽心思定义了一个<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;
    }
}

我本来要使用已弃用的方法FormsAuthentication.Authenticate。感谢您在这里放置了该类。 - strider

3
我不确定你是否尝试过,但是.... FormsAuthentication.Authenticate 负责为您执行此操作(尽管它现在已被弃用,因为建议使用 Membership 对象)。
根据 MSDN 的说明:
Authenticate 方法验证存储在应用程序配置文件的凭据部分中的用户凭据。或者,您可以使用 ASP.NET 成员资格来存储用户凭据并调用 ValidateUser 来验证这些凭据。
您还可以删除成员提供程序(因为即使您没有在 web.config 中声明它们,它们也会从 machine.config 文件继承)。
  <membership>
    <providers>
      <remove name="AspNetSqlMembershipProvider"/>
    </providers>
  </membership>

3
你需要编写自己的提供程序。可以参考 MSDN文档 中的示例 ReadOnlyXmlMembershipProvider,将其改为从 web.config 读取用户和凭据,而不是外部 XML 文件。请注意保留 HTML 标签。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接