public async Task <EmailConfirmedVM> ConfirmEmail(string userId, string code) { // Validation if (string.IsNullOrEmpty(userId) || string.IsNullOrEmpty(code)) { return(null); } // Result EmailConfirmedVM emailConfirmedVM = new EmailConfirmedVM(); User user = await userManager.FindByIdAsync(userId); if (user == null) { logger.LogWarning("User not found during email confirmation", userId); throw new ConfirmEmailFailedException("invalid"); } IdentityResult result = await userManager.ConfirmEmailAsync(user, code); if (result.Succeeded) { // Set claims of user List <Claim> claims = new List <Claim>() { new Claim(JwtRegisteredClaimNames.NameId, user.Id.ToString().ToUpper()), new Claim(JwtRegisteredClaimNames.UniqueName, user.UserName), new Claim(JwtRegisteredClaimNames.Email, user.Email), new Claim(JwtRegisteredClaimNames.Iat, DateTime.UtcNow.ToString(CultureInfo.CurrentCulture)) }; // TODO: Custom fields if (!string.IsNullOrEmpty(user.FirstName)) { claims.Add(new Claim(JwtRegisteredClaimNames.GivenName, user.FirstName)); } if (!string.IsNullOrEmpty(user.LastName)) { claims.Add(new Claim(JwtRegisteredClaimNames.FamilyName, user.LastName)); } // Email confirmation successful => Generate JWT token based on the user's claims string token = this.GenerateJWT(claims); emailConfirmedVM.Token = token; emailConfirmedVM.User = mapper.Map <User, UserVM>(user); return(emailConfirmedVM); } logger.LogWarning("Email confirmation is invalid", user); throw new ConfirmEmailFailedException("invalid"); }
public async Task <ActionResult <EmailConfirmedVM> > ConfirmEmail([FromQuery] string id, [FromQuery] string code) { // Validation if (!ModelState.IsValid || string.IsNullOrEmpty(id) || string.IsNullOrEmpty(code)) { return(BadRequest(ModelState)); } EmailConfirmedVM emailConfirmedVM = await this.bll.ConfirmEmail(id, code); return(Ok(emailConfirmedVM)); }