ASP.NET Identity 2.0 check if current user is in role IsInRole

允我心安 提交于 2019-11-27 22:54:23

The correct way in ASP Identity is as simple as

User.IsInRole("rolename");
Robert Goldwein

Assuming you are in ASP.NET, it's pretty simple:

if (!Roles.IsUserInRole(User.Identity.Name, "Administrators"))
{
  return "You are not authorized to access this page.";
)

(from http://msdn.microsoft.com/en-us/library/4z6b5d42%28v=vs.110%29.aspx)

You can get the user id from the Identity rather than having to lookup the user in the database...

var um = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new DbContext()));
var inrole = um.IsInRole(Context.User.Identity.GetUserId(), "Admin");

this worked for me hope this helps...

If HttpContext.Current.User.IsInRole("admin") Then
        adminmnu.Visible = True
    End If
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!