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

I have an ASP.NET application that uses the Roles.GetRolesForUser method. The penalty call works in the application, but when I use the same call in the reference library, it throws an exception. Exception Message:

Object reference not set to object instance

The strange thing is that when I check the roles, it is created.

My code is as follows:

 var roles = Roles.GetRolesForUser(userName); 

Any suggestions?

+6
source share
2 answers

NOTE: according to the OP comment itself, this answer solved its problem.

This is a .NET error .
To solve this problem, call:

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

here is a very similar question.

+4
source

In MVC 5, you can get user roles using the GetRoles(Id) method.
But before using GetRoles you need to make a userManager object:

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

Source: https://habr.com/ru/post/957481/


All Articles