从web.config获取允许用户

时间:2014-09-17 06:36:38

标签: c# asp.net asp.net-authorization

我尝试从web.config中获取允许的用户来访问该应用程序并拒绝其他用户并将其重定向到另一个页面。这是我的代码:

配置

<authentication mode="Windows"/>
<authorization>
  <allow users="anwar,abdulaziz"/>
  <deny users="?"/>
</authorization>

代码

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)
        {
            Response.Redirect("UnauthorizedUsers.aspx");
        }
    }
}

1 个答案:

答案 0 :(得分:1)

据我了解,您希望将未经授权的用户重定向到另一个页面。

protected void Application_EndRequest(Object sender, EventArgs e)
{
    if (HttpContext.Current.Response.Status.StartsWith("401"))
    {
        HttpContext.Current.Response.ClearContent();
        Response.Redirect("UnauthorizedUsers.aspx");
    }
}