【问题标题】:web.config forms authentication get all usersweb.config 表单身份验证获取所有用户
【发布时间】:2011-04-26 12:34:50
【问题描述】:

我有一个非常简单的 asp.net 应用程序,我正在使用表单身份验证并将每个用户的名称/密码存储在 web.config 中:

<authentication mode="Forms">
    <forms name="appNameAuth" path="/" loginUrl="l.aspx" protection="All" timeout="600">
        <credentials passwordFormat="Clear">
          <user name="user1" password="pass1"/>
          <user name="user2" password="pass2"/>
          <user name="user3" password="pass3"/>   
        </credentials>
    </forms>
</authentication>

我想简单地获取所有用户,然后将他们绑定到下拉列表。

【问题讨论】:

    标签: asp.net forms web-config forms-authentication


    【解决方案1】:

    你可以从http://msdn.microsoft.com/en-us/library/4c2kcht0.aspx这样的代码中读取web.config

    我从msdnhttp://msdn.microsoft.com/en-us/library/system.web.configuration.formsauthenticationconfiguration.credentials.aspx找到的

    FormsAuthenticationCredentials currentCredentials = formsAuthentication.Credentials;
    StringBuilder credentials = new StringBuilder();
    for (System.Int32 i = 0; i < currentCredentials.Users.Count; i++)
    {
        credentials.Append("Name: " + currentCredentials.Users[i].Name + " Password: " + currentCredentials.Users[i].Password);
        credentials.Append(Environment.NewLine);
    }
    

    【讨论】:

    • 谢谢。我在执行此操作时遇到了麻烦,因为它是从我的根 web.config 中提取的。
    • 您应该使用它来打开请求的 web.config 的表单配置:FormsAuthenticationConfiguration formsAuthentication= ((AuthenticationSection)WebConfigurationManager.OpenWebConfiguration("~").GetSection("system.web/authentication")).Forms;
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-20
    • 1970-01-01
    • 1970-01-01
    • 2015-06-18
    • 1970-01-01
    相关资源
    最近更新 更多