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

查看:95
本文介绍了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天全站免登陆