net成员-如何以编程方式确定用户的角色

时间:2021-11-05 12:44:04

What is the code for determining if a user is in a role?

确定用户是否在角色中的代码是什么?

I have set up all the users through the ASP.NET Configuration Security tab but now want to put logic around some key areas so only people in certain roles can see and access these areas.

我已经通过ASP建立了所有的用户。NET Configuration Security选项卡,但是现在希望将逻辑放在一些关键区域,这样只有特定角色的人才能看到和访问这些区域。

4 个解决方案

#1


23  

if (User.IsInRole("rolename")) {
  // my action
}

#2


8  

Easy~

简单~

HttpContext.Current.User.IsInRole("roleName")

#3


3  

Check out the Roles class, specifically IsUserInRole, GetUsersInRole, AddUserToRole, etc.

查看Roles类,特别是IsUserInRole、GetUsersInRole、AddUserToRole等。

I use these all the time.

我一直在用这个。

#4


1  

thanks to "Chris Van Opstal". i solved my problem like this way,

感谢“Chris Van Opstal”。我这样解决了我的问题,

    public ActionResult Index()
    {

        if (User.IsInRole("Supervisor"))
        {
            return RedirectToAction("Index", "InvitationS");
        }
        return View();
    }

#1


23  

if (User.IsInRole("rolename")) {
  // my action
}

#2


8  

Easy~

简单~

HttpContext.Current.User.IsInRole("roleName")

#3


3  

Check out the Roles class, specifically IsUserInRole, GetUsersInRole, AddUserToRole, etc.

查看Roles类,特别是IsUserInRole、GetUsersInRole、AddUserToRole等。

I use these all the time.

我一直在用这个。

#4


1  

thanks to "Chris Van Opstal". i solved my problem like this way,

感谢“Chris Van Opstal”。我这样解决了我的问题,

    public ActionResult Index()
    {

        if (User.IsInRole("Supervisor"))
        {
            return RedirectToAction("Index", "InvitationS");
        }
        return View();
    }