Logic behind UserManager.Get{Property}Async methods RRS feed

  • Question

  • User700032381 posted

    When looking through scaffolded Identity code, I see things like this pretty frequently:

    var user = await _userManager.GetUserAsync(User); // gets user from ClaimsPrincipal
    // ... later on...
    var email = await _userManager.GetEmailAsync(user);

    If we already have the IdentityUser, why do we later go back to the _userManager and make a whole new database call? We already have the IdentityUser.Email property in our method.

    One section of scaffolded code where this happens is in the Identity/Pages/Account/Manage/Email.cshtml.cs file. The IdentityUser is fetched using the ClaimsPrincipal, and then both the user ID and email address are then retrieved from the database instead of the IdentityUser instance. This adds two extra async calls that, at face value, are entirely unnecessary because the data is already present in that context.

    Monday, May 24, 2021 6:24 PM

All replies