【问题标题】:Asp.Net Core Identity, IdentityUser.Email versus UserManage<IdentityUser>.GetEmailAsync(IdentityUser user)Asp.Net Core Identity、IdentityUser.Email 与 UserManage<IdentityUser>.GetEmailAsync(IdentityUser 用户)
【发布时间】:2020-05-29 06:30:38
【问题描述】:

我正在查看 Asp.Net Core 网站项目中的脚手架身份帐户管理页面。更改电子邮件 Razor 页面中的 OnGetAsync() 使用 UserManager.GetUserAsync() 加载当前用户。 然后它调用另一个方法将电子邮件地址和 IsEmailConfirmed 值加载到页面模型的属性中。

加载方法使用 UserManager.GetEmailAsync(user) 而不仅仅是 user.Email。为什么微软编码员会这样做?

通过使用 UserManager.IsEmailConfirmedAsync(user) 而不仅仅是 user.IsEmailConfirmed,它对 IsEmailConfirmed 执行相同的操作。

我错过了什么?

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; }
  }
}```

【问题讨论】:

    标签: c# asp.net-core asp.net-identity


    【解决方案1】:

    这一定是一个实施问题。查看github 的UserManager 类,GetEmailAsync 向数据库和二人组发出请求。这是不正确的,因为第一个请求已经检索了有关用户的所有内容。无论如何,您都可以自己实现。

    【讨论】:

      猜你喜欢
      • 2017-09-20
      • 1970-01-01
      • 2014-06-25
      • 2019-05-10
      • 2018-04-25
      • 1970-01-01
      • 2019-07-24
      • 1970-01-01
      • 2018-01-15
      相关资源
      最近更新 更多