UserManager.FindByName does not return a role

I am using OpenIddict to validate a token . Yesterday, when I call userManager.FindByNameAsync(request.Username) , I get a User with roles.
Today I get a user with the Roles property count = 0.

I tried loading roles using await userManager.GetRolesAsync(user); , and I get an array with a score of 3. This means that the user has roles.

I do not know what has changed, but how can I load a user using the FindByNameAsync function?

Full code:

 [HttpPost("token"), Produces("application/json")] public async Task<IActionResult> Exchange(OpenIdConnectRequest request) { Debug.Assert(request.IsTokenRequest(), "The OpenIddict binder for ASP.NET Core MVC is not registered. " + "Make sure services.AddOpenIddict().AddMvcBinders() is correctly called."); if (request.IsPasswordGrantType()) { var user = await userManager.FindByNameAsync(request.Username); //roles count is 0 if (user == null) { return BadRequest(new OpenIdConnectResponse { Error = OpenIdConnectConstants.Errors.InvalidGrant, ErrorDescription = "The email/password couple is invalid." }); } var roles = await userManager.GetRolesAsync(user); //roles count is 3 
+5
source share
1 answer

but how can I load a user with roles using the FindByNameAsync function?

You cannot (at least not implement your own IUserStore ).

Under the hood, the default implementation of the ASP.NET core identifier store (based on EF) does not load the navigation properties associated with the user object for performance reasons, so the Roles property is not populated.

To load user-related roles, use UserManager.GetRolesAsync(user); .

+6
source

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


All Articles