asp.net – .NET身份电子邮件/用户名更改

前端之家收集整理的这篇文章主要介绍了asp.net – .NET身份电子邮件/用户名更改前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
有谁知道如何让用户使用电子邮件确认更改使用ASP.NET身份的用户名/电子邮件?有很多关于如何更改密码的例子,但是我找不到任何内容

解决方法

这应该为你做点窍门:
  1. // get user object from the storage
  2. var user = await userManager.FindByIdAsync(userId);
  3.  
  4. // change username and email
  5. user.Username = "NewUsername";
  6. user.Email = "New@email.com";
  7.  
  8. // Persiste the changes
  9. await userManager.UpdateAsync(user);
  10.  
  11. // generage email confirmation code
  12. var emailConfirmationCode = await userManager.GenerateEmailConfirmationTokenAsync(user.Id);
  13.  
  14. // generate url for page where you can confirm the email
  15. var callbackurl= "http://example.com/ConfirmEmail";
  16.  
  17. // append userId and confirmation code as parameters to the url
  18. callbackurl += String.Format("?userId={0}&code={1}",user.Id,HttpUtility.UrlEncode(emailConfirmationCode));
  19.  
  20. var htmlContent = String.Format(
  21. @"Thank you for updating your email. Please confirm the email by clicking this link:
  22. <br><a href='{0}'>Confirm new email</a>",callbackurl);
  23.  
  24. // send email to the user with the confirmation link
  25. await userManager.SendEmailAsync(user.Id,subject: "Email confirmation",body: htmlContent);
  26.  
  27.  
  28.  
  29. // then this is the action to confirm the email on the user
  30. // link in the email should be pointing here
  31. public async Task<ActionResult> ConfirmEmail(string userId,string code)
  32. {
  33. var confirmResult = await userManager.ConfirmEmailAsync(userId,code);
  34.  
  35. return RedirectToAction("Index");
  36. }

猜你在找的asp.Net相关文章