0

嗨,我在为 Web 应用程序设置管理模块时遇到问题

  1. 我的系统管理员和技术人员有两个角色

  2. 如果转到管理员模块,系统将查看他是否是管理员

  3. 如果用户不是管理员,则将他重定向到页面

    抱歉,您无权访问此页面!

我使用 Web.config 来限制对子目录 Admin 的访问

<?xml version="1.0"?>
<configuration>
    <system.web>
      <authorization>
        <allow roles="admin" />
        <deny users="*"/>
      </authorization>
    </system.web>
</configuration>

我也有 C# 代码来检查登录用户是管理员还是其他

protected void Page_Load(object sender, EventArgs e)
    {
        if (HttpContext.Current.User.Identity.IsAuthenticated)
        {
            if (Page.User.IsInRole("admin"))
            {
                if (!Page.IsPostBack)
                {
                    DisplayRolesInGrid();
                }
            }
            if(!Page.User.IsInRole("admin"))
            {
                Response.Redirect("/accessPage.aspx");
            }

        }
    }
4

2 回答 2

1

不要将这两种类型的角色管理相互混淆。它们相互排斥。一个在 web.config 中,另一个在 C# 代码中。只需删除 web.config 访问部分并使用 codein Page_Load 功能,就像您已经完成的那样。

protected void Page_Load(object sender, EventArgs e)
{

        if (Page.User.IsInRole("admin"))
        {
            // all is good, do not do anything
            // if you want to initialized something, do it here
        }
        else
        {
            // opps you do not have access here, take him somewhere else
            Response.Redirect("/accessPage.aspx");
        }


}
于 2012-08-23T15:53:08.247 回答
0

不知道你的问题是什么,但我至少可以看到一个问题: <deny users="*"/>意味着拒绝所有人。应该将<deny users="?" />未经身份验证的人拒之门外,然后<deny roles="Tech" />确保不允许您的技术用户使用。

于 2012-08-23T15:46:17.183 回答