C# 从web.config获取允许用户

C# 从web.config获取允许用户,c#,asp.net,asp.net-authorization,C#,Asp.net,Asp.net Authorization,我试图从web.config获得一个允许用户访问应用程序的权限,拒绝其他用户访问,并将他们重定向到另一个页面。这是我的代码: 配置 <authentication mode="Windows"/> <authorization> <allow users="anwar,abdulaziz"/> <deny users="?"/> </authorization> 代码 AuthorizationSection config

我试图从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");
        }
    }
}
AuthorizationSection configSection=(AuthorizationSection)
ConfigurationManager.GetSection(“system.web/authorization”);
var users=新列表();
var rules=configSection.rules;
foreach(规则中的授权规则)
{
if(rule.Action!=AuthorizationRuleAction.Allow)
{
foreach(rule.Users中的字符串用户)
{
响应。重定向(“UnauthorizedUsers.aspx”);
}
}
}

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

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

它起作用了吗?它不起作用吗?有错误吗?问题是什么?如果用户被允许或不允许,它总是重定向到UnauthorizedUsers.aspx如果您得到无限重定向循环,这表明未授权用户无法访问您的UnauthorizedUsers.aspx。您必须将其从常规授权规则中排除,方法是将其他文件放入子文件夹并保护该子文件夹(而不是整个项目),或者在中添加单独的授权规则。如果我的答案对您有帮助,请向上投票我的答案(如果您有代表)然后按分数下面的标记接受它。这是一种帮助