Roles.GetRolesForUser throws exception Object reference not set to an instance of an object Roles.GetRolesForUser throws exception Object reference not set to an instance of an object asp.net asp.net

Roles.GetRolesForUser throws exception Object reference not set to an instance of an object


NOTE: according to the OP's comment himself, this answer solved his problem.

This is a .NET Framework bug.
To solve this issue call:

string[] roles = Roles.Provider.GetRolesForUser(userName);

here is a very similar question.


In MVC 5 you can get user roles with GetRoles(Id) method.
But, before use GetRoles you have to make userManager object:

var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));var d = userManager.GetRoles(5);