简体   繁体   中英

Asp.Net Core Identity, IdentityUser.Email versus UserManage<IdentityUser>.GetEmailAsync(IdentityUser user)

I am reviewing the scaffolded identity account management pages in an Asp.Net Core website project. The OnGetAsync() in the change email Razor page loads the current user using UserManager.GetUserAsync(). It then calls another method to load the email address and IsEmailConfirmed value into properties of the page model.

The load method uses UserManager.GetEmailAsync(user) instead of just user.Email. Why would the Microsoft coders do that?

It does the same thing for the IsEmailConfirmed by using UserManager.IsEmailConfirmedAsync(user) instead of just user.IsEmailConfirmed.

What am I missing?

public partial class Email : PageModel
{
  public string Email { get; set; }

  [BindProperty]
  public InputModel Input { get; set; }
  public bool IsEmailConfirmed { get; set; }

  public async Task<IActionResult> OnGetAsync()
  {
    var user = await _userManager.GetUserAsync(User);
    if (user == null)
    {
      return NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
    }
    // user should be valid here.
    await LoadAsync(user);
    return Page();
  }

  protected async Task LoadAsync(IdentityUser user)
  {
    // user should be valid here.

    // Why not just "string email = user.Email"?
    string email = await _userManager.GetEmailAsync(user);
    Email = email;
    Input = new InputModel { NewEmail = email };

    // Why not just do "IsEmailConfirmed = user.IsEmailConfirmed"?
    IsEmailConfirmed = await _userManager.IsEmailConfirmedAsync(user);
    }

  public class InputModel
  {
  [Required]
  [EmailAddress]
  [Display(Name = "New email")]
  public string NewEmail { get; set; }
  }
}```

That has to be an implementation issue. Looking at the UserManager class at github , the GetEmailAsync makes a request to the database and also the duo. Which is not proper since the first request already retrieves everything about the user. You can implement that yourself anyway.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM