简体   繁体   中英

Change email using UserManager.ChangeEmailAsync()

I'm trying to give my users the ability to change email. I'd like to send a verification email as well, in which they can verify/confirm their email.

I'd just like to know more about the flow of this, and I haven't been able to find reasonable documentation online.

I see the flow like this:

  1. User enters the new email they wish to use
  2. Code/Token is created together with the confirmation email (the new email is not yet applied to the user)
  3. Confirmation email is sent to the new email
  4. User confirms/verifies their new email
  5. New email and code is received in the controller and the UserManager.ChangeEmailAsync(User user, string newEmail, string code) is invoked

Is the new email applied to the user when the ChangeEmailAsync() method is invoked, or do I have to apply the new email before sending the confirmation email ( set EmailConfirmed back to false )?

try this: tring code = await UserManager.GenerateUserTokenAsync("ChangeEmail",userID); in SendingEmail() to the new email and save the new email in a temporary table

the function when the user confirm the new e-mail: `

             public async Task<IHttpActionResult> ChangeEmail(ChangeEmailModel model) 
             {
                   try
                   {
                HttpUtility.UrlEncode(model.Code);                   
                if ( !UserManager.VerifyUserToken(model.UserId, "ChangeEmail", model.Code)) //to verify the code
                {
                    _logger.Error($"token expired");
                    return ResponseMessage(Request.CreateResponse(HttpStatusCode.BadRequest, new KeyValuePair<String, String>(Messages.ExpiredLink, CommonMessages.ExpiredLink)));
                }
                else
                {
                    UserDetailsManager userDetailsManager = new UserDetailsManager();
                    string Email = userDetailsManager.GetNewEmail(model.UserId);//get the new email from the temporary table
                    var user = await UserManager.FindByIdAsync(model.UserId);
                    user.Email = Email;//change the email
                    user.UserName = Email;
                    result = await UserManager.UpdateAsync(user);
                    if (!result.Succeeded)
                    {
                        foreach (var item in result.Errors)
                        {
                            if (item.Contains("already"))
                            {
                                _logger.Info("In ChangeEmail user already exists");
                                return ResponseMessage(Request.CreateResponse(HttpStatusCode.BadRequest, new KeyValuePair<String, String>(Messages.EmailUserExist, CommonMessages.EmailUserExist)));
                            }
                        }
                    }

                }

            }

        }
        catch (Exception ex)
        {
            _logger.Error($"In ChangeEmail Error - {ex.Message} return {HttpStatusCode.InternalServerError}");
            return ResponseMessage(Request.CreateResponse(HttpStatusCode.InternalServerError, new KeyValuePair<String, String>(Messages.InternalServerError, CommonMessages.InternalServerError)));
        }
        _logger.Info($"ChangeEmail end status {HttpStatusCode.OK} ");
        return Ok("Success");
    }`

this function also Overrides the preoccupation with the confirmEmail

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM