11

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

我已经通过 ASP.NET 配置安全选项卡设置了所有用户,但现在想在一些关键区域周围放置逻辑,以便只有特定角色的人才能看到和访问这些区域。

4

4 回答 4

23
if (User.IsInRole("rolename")) {
  // my action
}
于 2009-07-31T17:57:55.057 回答
8

轻松~

HttpContext.Current.User.IsInRole("roleName")
于 2009-07-31T17:58:34.773 回答
3

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

我一直在使用这些。

于 2009-07-31T17:58:05.867 回答
1

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

    public ActionResult Index()
    {

        if (User.IsInRole("Supervisor"))
        {
            return RedirectToAction("Index", "InvitationS");
        }
        return View();
    }
于 2014-07-03T13:46:35.480 回答