[英]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: 我看到这样的流程:
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.