2011-04-26 36 views
2

我有我在web.config中使用窗體身份驗證和存儲每個用戶名/密碼,一個很簡單的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

您可以從代碼像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); 
} 
+0

感謝。我遇到了麻煩,因爲它從我的根web.config中拉出。 – dave2118 2011-04-26 13:48:30

+0

你應該使用這個來打開請求的web.config表單配置: FormsAuthenticationConfiguration formsAuthentication =((AuthenticationSection)WebConfigurationManager.OpenWebConfiguration(「〜」)。GetSection(「system.web/authentication」))。Forms ;' – Myobis 2015-11-18 21:28:11

相關問題