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

user3530155 picture user3530155 · Apr 14, 2014 · Viewed 34.6k times · Source

With ASP.NET Identity 2.0 how do you check if the currently logged on user is in a role? I am using the following, but wondering if there is something more efficient.

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

if (inrole)
{
}

Answer

bluee picture bluee · May 28, 2015

The correct way in ASP Identity is as simple as

User.IsInRole("rolename");