如何管理用户访问级别

时间:2016-02-28 07:39:42

标签: c# asp.net

这是我的App_Code代码(Helper.cs) 我的目标是管理限制用户访问表单的用户级别:以下是我创建的代码:

 public static void ValidateAccess(int[] usertypes)
{
    // Checks if the user has logged in already
    if (HttpContext.Current.Session["userid"] != null &&
        HttpContext.Current.Session["usertypeid"] != null)
    {
        int usertypeid = int.Parse(HttpContext.Current.Session["usertypeid"].ToString());

        if (usertypes.Contains(usertypeid))
        {
            // User is authorized to access the page.
        }
        else
        {
            // User is not authorized to access the page.
            HttpContext.Current.Response.Redirect("~/NotAuthorized.aspx");
        }
    }
    else // if the user has not logged in yet
        HttpContext.Current.Response.Redirect("~/login.aspx?url" +
            HttpContext.Current.Request.Url.AbsoluteUri);
}

PageLoad活动

  protected void Page_Load(object sender, EventArgs e)
{
    Helper.ValidateAccess(new[] { 1 });
}

当我登录这两个用户时,他们都重定向到NotAuthorize.aspx?谁能告诉我我的代码有什么问题?

0 个答案:

没有答案