web.config表单身份验证获取所有用户

时间:2011-04-26 12:34:51

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

我有一个非常简单的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>

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

1 个答案:

答案 0 :(得分:1)

您可以从http://msdn.microsoft.com/en-us/library/4c2kcht0.aspx

这样的代码中读取web.config

我是从msdn http://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);
}