Search code examples
c#asp.net-web-apiasp.net-identity-2

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


I have the following function that re-generate a confirmation email (to complete registration) in case of an expired 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();
    }

How can I get the email of the user from the webUsers table based on his userid?


Solution

  • You can fetch user from database using UserManager's FindByIdAsync and then get the email

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