我使用web.config中的以下代码实现了我的asp.net网站的访问限制
<authentication mode="Windows">
</authentication>
<authorization>
<allow users="na\user1, phil\user2, so on" />
<deny users="*"/>
</authorization>
我应该为其提供一个界面(网页或modalpopup)的管理员用户很少,他们可以在这里看到当前用户,删除用户或添加用户来访问此站点。现在我想知道
1)如何以编程方式查找当前用户,即用户属性值。
我使用asp.net How do I reference authorized users hard coded in web.config in my code
中的以下代码解决了问题AuthorizationSection configSection =
(AuthorizationSection)ConfigurationManager.GetSection("system.web/authorization");
var users = new List<string>();
var rules = configSection.Rules;
foreach (AuthorizationRule rule in rules)
{
if (rule.Action == AuthorizationRuleAction.Allow)
{
foreach (string user in rule.Users)
{
if (!users.Contains(user))
{
users.Add(user);
}
}
}
//rule.Users.Add("phil\user3");
//rule.Users.Remove("phil\\user2");
}
2)如何通过在网站运行时删除或添加用户来更新web.config?
我尝试使用上面代码中的注释行添加和删除用户,但这不起作用。
我的错误代码在哪里?正如我所说,我需要动态添加或删除web.config中的用户,以便任何人都可以提供一些代码。 提前致谢!