根据用户的 userID .net 身份获取用户的电子邮件地址

Get user's email address based on his userID .net identity

我有以下功能,在过期的情况下重新生成确认电子邮件(以完成注册)link。

    [AllowAnonymous]
    [HttpGet]
    [Route("ResendConfirmationEmail")]
    public async Task<IHttpActionResult> ResendConfirmationEmail(string userId = "")
    {
        if (string.IsNullOrWhiteSpace(userId))
        {
            ModelState.AddModelError("", "User Id is required");
            return BadRequest(ModelState);
        }
        if (userId.GetHashCode() != null)
        {
            var code = await UserManager.GenerateEmailConfirmationTokenAsync(userId);
            var callbackUrl = new Uri(Url.Link("ConfirmEmailRoute", new { userId, code = code }));
            string subject = "Please confirm your account";
            string body = "Please confirm your account by clicking this : <a href=\"" + callbackUrl + "\">link</a>";
            SendEmail(?????, callbackUrl, subject, body);
        }
        return Ok();
    }

如何根据 useridwebUsers table 获取用户的电子邮件?

您可以使用 UserManager 的 FindByIdAsync 从数据库中获取用户,然后获取电子邮件

var user = await UserManager.FindByIdAsync(userId); 
var email = user.Email;