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

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


The correct way in ASP Identity is as simple as

User.IsInRole("rolename");


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");