7

I am using OpenIddict for token authentication. Yesterday when I call userManager.FindByNameAsync(request.Username) I get User with Roles.
Today I get user with Roles property count = 0.

I tried to load roles with await userManager.GetRolesAsync(user); and I get array with count 3. That means user has roles.

I do not know what changed, but how can I load user with roles with FindByNameAsync function?

Complete 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
Makla
  • 9,899
  • 16
  • 72
  • 142

1 Answers1

7

but how can I load user with roles with FindByNameAsync function?

You can't (at least not without implementing your own IUserStore).

Under the hood, the default ASP.NET Core Identity store implementation (based on EF) doesn't eagerly load the navigation properties associated with the user entity for performance reasons, which is why the Roles property is not populated.

To load the roles associated with a user, use UserManager.GetRolesAsync(user);.

Kévin Chalet
  • 39,509
  • 7
  • 121
  • 131
  • 3
    Why can't i just type .Include(Claims) etc. and ACTUALLY GET IT WOW – Christo S. Christov Oct 28 '17 at 20:13
  • I don't think insults are necessary to express your opinion. If you want to use the `Include` extension, you have to create your own store (which can be derived from the official EF store) and override the methods that return a user instance. – Kévin Chalet Oct 28 '17 at 20:22
  • It's just completely BADLY created. You retrieve half of your properties in some way - half the other. Who thought that this idea is good!? Performance? You won't have performance when you give cancer to half of your dev community – Christo S. Christov Oct 28 '17 at 20:24
  • 1
    On the other hand, loading all the navigation properties each time a user is retrieved also has a performance impact, specially when you don't need them. – Kévin Chalet Oct 28 '17 at 20:29