Skip to main content
 首页 » 编程设计

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

2024年04月17日41shanyou

我有以下功能,可以在链接过期时重新生成确认电子邮件(以完成注册)。

    [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(); 
    } 

我如何根据他的 userid 从 webUsers 表中获取用户的电子邮件?

请您参考如下方法:

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

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