U have an ASP.Net MVC 5 website and I want to retrieve current user's roles (if any) and act on them accordingly. I've noticed some changes, even after the Beta version of VS 2013 in the template. I'm currently using this code:
//in Utilities.cs class
public static IList<string> GetUserRoles(string id)
{
if (id == null)
return null;
var UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new AppContext()));
return UserManager.GetRoles(id);
}
//and I call it like this:
var roles = Utilities.GetUserRoles(User.Identity.GetUserId());
Is this the best approach? If not, what is?
Edit:
I'm using this to create the roles and add users to the role:
RoleManager.Create(new IdentityRole("admin"));
if (um.Create(user, password).Succeeded)
{
UserManager.AddToRole(user.Id, role);
}