简体   繁体   English

使用UserManager.ChangeEmailAsync()更改电子邮件

[英]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 控制器中收到新的电子邮件和代码UserManager.ChangeEmailAsync(User user, string newEmail, string code)并调用UserManager.ChangeEmailAsync(User user, string newEmail, string code)

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 )? 在调用ChangeEmailAsync()方法时是否将新电子邮件应用于用户,或者在发送确认电子邮件之前是否必须应用新电子邮件( EmailConfirmed设置为false )?

try this: tring code = await UserManager.GenerateUserTokenAsync("ChangeEmail",userID); 试试这个: tring code = await UserManager.GenerateUserTokenAsync("ChangeEmail",userID); in SendingEmail() to the new email and save the new email in a temporary table 在SendingEmail()中新邮件并将新邮件保存在临时表中

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 此功能还覆盖了对confirmEmail的关注

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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