根据角色重定向用户?

时间:2012-08-23 15:41:29

标签: c# asp.net web-config

您好我在设置网络应用管理模块

时遇到问题
  1. 我在System Admin和Tech
  2. 上有两个角色
  3. 如果转到管理模块,系统将会看到他是否是管理员
  4. 如果用户不是管理员,则会将他重定向到页面

      

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

  5. 我使用Web.config限制子目录Admin

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

    我也有C#代码来检查登录用户是否为admin或其他

    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");
                }
    
            }
        }
    

2 个答案:

答案 0 :(得分: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");
        }


}

答案 1 :(得分:0)

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

相关问题