【问题标题】:Can't Detect User Role无法检测用户角色
【发布时间】:2015-10-23 07:02:47
【问题描述】:

使用 asp.net 和 c# 以及 Visual Studio 2010 我有一个登录页面和一个登录控件,我正在做一些事情,当用户尝试登录时,它将检测用户角色。这是我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Web.Security;

public partial class Login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Login1_LoggedIn(object sender, EventArgs e)
    {
        if (Session["Admin"] != null)
        {
            Response.Redirect("~/Admin/HomeAdmin.aspx");
        }
        else if (Session["Professor"] != null)
        {
            Response.Redirect("~/Professor/HomeProfessor.aspx");
        }
        else if (Session["Student"] != null)
        {
            Response.Redirect("~/Student/HomeStudent.aspx");
        }            
    }

    protected void Login1_LoggingIn(object sender, LoginCancelEventArgs e)
    {
        if (Roles.IsUserInRole("Administor"))
        {
            Session["Admin"] = Login1.UserName;
            //only run for admins
        }




        else if (Roles.IsUserInRole("Professor"))
        {
            Session["Professor"] = Login1.UserName;
            //only run for professors
        }





        else if (Roles.IsUserInRole("Student"))
        {
            Session["Student"] = Login1.UserName;
            //only run for students
        }
    }
}  

然后,当我登录时,它会检测到错误的角色,例如,我使用管理员用户登录,但它会将其检测为学生! 正如您在代码中看到的那样,它会将我重定向到页面 (HomeStudent.aspx)。

这是我的角色经理的视图:Click here to see the image of my role manager

你认为是什么问题,我该怎么办?!!

【问题讨论】:

  • 为什么不将角色放在一个Session["Role"] 中,并在每次登录时分配不同的值。然后,在 Login1_LoggingIn 事件中,您可以先使用 Session.Clear() 重置您的会话,然后将其分配给新值
  • 你的意思是制作3个登录页面? (每个角色一个登录页面?)
  • 不,一点也不。只是 Session["Role"] 将有三个值,根据登录的角色,像这样:if (Roles.IsUserInRole("Administor")) { Session["Role"] ="Administrator"; //only run for admins } else if (Roles.IsUserInRole("Professor")) { Session["Role"] = "Professor"; //only run for professors } 然后调用 Session 的值,而不是检查它是否是 null。我没有详细检查上面的代码,但可能这应该工作
  • 我按照你说的做了,但我还是有问题。我认为因为主要问题是:当编译器正在读取 IF 部分时
  • 等一下,我认为我的问题已经解决了我的错误是我忘记在 user.isinrole 之前放一个(login1.username)!

标签: c# asp.net visual-studio-2010 roleprovider login-control


【解决方案1】:

在发布登录表单时触发事件LoggingIn,但在用户通过身份验证之前(检查msdn)。

您应该检查Roles.IsUserInRole("yourRole") LoggedIn 事件而不是LoggingIn

【讨论】:

  • 我第一次做的是你说的那件事……但我还是有这个问题
  • 如果你在 LoggedIn 事件中运行Roles.GetRolesForUser() ,它会返回什么?
  • 你的意思是在登录的事件中写入:Roles.GetRolesForUsers() 或将其设置为等于某物(例如标签)?
  • @mohammadtalaie 仍然有这个问题?
  • 是的...仍在尝试修复它不知道可以解决这个问题?
【解决方案2】:

我找到了解决方案并通过将代码更改为此解决了我的问题:

 if (Roles.IsUserInRole(Login1.UserName , "Administor"))
    {
        Session["Admin"] = Login1.UserName;
        Response.Redirect("~/Admin/HomeAdmin.aspx");       
        //only run for admins

    }


    else if (Roles.IsUserInRole(Login1.UserName , "Professor"))
    {
        Session["Professor"] = Login1.UserName;
        Response.Redirect("~/Professor/HomeProfessor.aspx");
        //only run for professors
    }



    else if (Roles.IsUserInRole(Login1.UserName , "Student"))
    {
        Session["Student"] = Login1.UserName;
        Response.Redirect("~/Student/HomeStudent.aspx"); 
        //only run for students
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-14
    • 2012-12-05
    • 2021-09-29
    • 2012-11-13
    • 2016-03-09
    • 2020-04-27
    • 2015-11-05
    • 1970-01-01
    相关资源
    最近更新 更多