asp.net membership - how to determine programmatically is user is in role

送分小仙女□ 提交于 2019-12-18 05:43:46

问题


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.


回答1:


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



回答2:


Easy~

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



回答3:


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

I use these all the time.




回答4:


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

    public ActionResult Index()
    {

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


来源:https://stackoverflow.com/questions/1213964/asp-net-membership-how-to-determine-programmatically-is-user-is-in-role

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!