AspNet Core 生成和更改电子邮件地址 [英] AspNet Core Generate and Change Email Address

查看:26
本文介绍了AspNet Core 生成和更改电子邮件地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现一种方法让用户在 AspNetCore 中更改他们的电子邮件,因此在帐户管理屏幕上我有一个更改功能,它将在用户管理器上调用 GenerateChangeEmailTokenAsync,然后发送包含令牌和 UserId 链接的电子邮件.

I am trying to implement a way for users to change their email in AspNetCore so on the Account Management screen I have the change function that will call GenerateChangeEmailTokenAsync on the user manager, then sends the email with the link containing the Token and UserId.

我的问题是如何允许将电子邮件地址更改为新地址的链接,因为 ChangeEmailAsync 需要输入新的电子邮件地址.

My problem is how do I allow the link the change the email address to the new address since ChangeEmailAsync requires the new email address be entered.

实现此功能的最佳实践方式是什么?我不想通过电子邮件链接中的新电子邮件地址发送,但也不想让他们再次键入电子邮件.希望有人这样做过,我无法在任何地方找到它,它应该很简单.

What is the best practice way of implementing this functionality? I do not want to send over the new email address in the email link but also do not want to make them type the email again. Hopefully someone has done this, I cannot find it anywhere only, and it should be very simple.

推荐答案

我知道现在回答这个问题有点晚了,但我之前一直在寻找它,并认为我将答案留给其他人.

I know it is late answering this, but I was looking for it myself before, and thought I leave the answer here for others.

GenerateChangeEmailTokenAsync 方法将新电子邮件作为令牌哈希的一部分.接下来,您创建一个包含令牌、新电子邮件和旧电子邮件的链接

The GenerateChangeEmailTokenAsync method takes the new email as part in the hash of the token. Next you create a link that contains the token, the new email and the old email

 var token = await _userManager.GenerateChangeEmailTokenAsync(user, model.NewEmail);
 var resetLink = Url.Action("ChangeEmailToken", "account", new {token = token, oldEmail = user.Email, newEmail = model.newEmail }, protocol: HttpContext.Request.Scheme);

接下来,您通过电子邮件将此链接发送给用户.

Next you send this link to the user in an email.

点击时,用户点击链接中指定的方法(此处为 AccountController 上的ChangeEmailToken":

When clicked, the user hits the method named in the link (here "ChangeEmailToken" on AccountController:

 [AllowAnonymous]
 [HttpGet]
 public async Task<IActionResult> ChangeEmailToken([FromQuery] string token, [FromQuery] string oldEmail, [FromQuery] string newEmail)

接下来,您需要验证令牌,并 - 如果成功 - 更新电子邮件地址.

Next you need to verify the token, and -if succesful- update the email address.

var result = await _userManager.ChangeEmailAsync(user, newEmail, token);

这篇关于AspNet Core 生成和更改电子邮件地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆