【问题标题】:Which Membership Provider implements users stored in web.config?哪个 Membership Provider 实现了存储在 web.config 中的用户?
【发布时间】:2012-09-14 13:47:31
【问题描述】:

有一个代码盲时刻。

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&lt;credentials&gt; 元素中的用户名和密码?

【问题讨论】:

  • 可能是因为默认的成员资格提供程序是 AspNetSqlProvider,它使用 SQL Server 数据库作为其用户存储。
  • 我希望这是一个临时解决方案。在 web.config 中维护凭据将变得非常困难,因为每次更改都会重新启动 Web 应用程序。
  • 当然。但是它适合手头的场景。我需要为密码永远不会更改的单个用户(曾经)为简单的应用程序提供微不足道的保护。具有更大依赖性的解决方案是不合适的。

标签: asp.net membership-provider


【解决方案1】:

我很惊讶,考虑到框架设计者如何费力地定义一个 &lt;credentials /&gt; 元素,他们没有实现任何代码来使用它。

我找到了这个here 的一种工作实现,我已经修复并包含在下面。 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。感谢您将课程放在这里。
【解决方案2】:

您将需要为此编写自己的提供程序。在MSDN documentation 中获取样本ReadOnlyXmlMembershipProvider 并将其更改为从web.config 而不是外部XML 文件读取用户和凭据应该相对简单。

【讨论】:

    【解决方案3】:

    我不确定你是否尝试过,但是......

    FormsAuthentication.Authenticate 负责为您执行此操作(尽管现在已弃用,因为建议的行为是使用 Membership 对象)

    来自 MSDN:

    Authenticate 方法验证存储在应用程序配置文件的凭据部分中的用户凭据。或者,您可以使用 ASP.NET 成员资格来存储用户凭据并调用 ValidateUser 来验证凭据。

    您还可以删除成员资格提供程序(因为即使您没有在 web.config 中声明它们,它们也是从 machine.config 文件继承的)

      <membership>
        <providers>
          <remove name="AspNetSqlMembershipProvider"/>
        </providers>
      </membership>
    

    【讨论】:

      【解决方案4】:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-29
        • 2011-03-08
        • 1970-01-01
        • 2013-09-24
        • 1970-01-01
        相关资源
        最近更新 更多