.NET身份电子邮件/用户名更改 [英] .NET Identity Email/Username change

查看:83
本文介绍了.NET身份电子邮件/用户名更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道如何使用户能够使用带有电子邮件确认的ASP.NET身份来更改用户名/电子邮件吗?关于如何更改密码,有很多示例,但是我找不到任何内容.

Does anyone know how to enable a user to change username/email with ASP.NET identity with email confirmation? There's plenty of examples on how to change the password but I can't find anything on this.

推荐答案

更新2017年12月在评论中提出了一些好的观点:

Update Dec 2017 Some good points have been raised in comments:

  • 在确认新邮件时,最好在单独的字段中填写-如果用户输入的电子邮件不正确.等待直到确认新电子邮件,然后将其作为主要电子邮件.请参阅下面来自Chris_的非常详细的答案.
  • 还有可能存在该电子邮件帐户已经存在的情况-请确保您也进行检查,否则可能会遇到麻烦.

这是一个非常基本的解决方案,不能涵盖所有可能的组合,因此请运用您的判断力,并确保您通读注释-那里的观点非常好.

This is a very basic solution that does not cover all possible combinations, so use your judgment and make sure you read through the comments - very good points have been raised there.

// get user object from the storage
var user = await userManager.FindByIdAsync(userId);

// change username and email
user.Username = "NewUsername";
user.Email = "New@email.com";

// Persiste the changes
await userManager.UpdateAsync(user);

// generage email confirmation code
var emailConfirmationCode = await userManager.GenerateEmailConfirmationTokenAsync(user.Id);

// generate url for page where you can confirm the email
var callbackurl= "http://example.com/ConfirmEmail";

// append userId and confirmation code as parameters to the url
callbackurl += String.Format("?userId={0}&code={1}", user.Id, HttpUtility.UrlEncode(emailConfirmationCode));

var htmlContent = String.Format(
        @"Thank you for updating your email. Please confirm the email by clicking this link: 
        <br><a href='{0}'>Confirm new email</a>",
        callbackurl);

// send email to the user with the confirmation link
await userManager.SendEmailAsync(user.Id, subject: "Email confirmation", body: htmlContent);



// then this is the action to confirm the email on the user
// link in the email should be pointing here
public async Task<ActionResult> ConfirmEmail(string userId, string code)
{
    var confirmResult = await userManager.ConfirmEmailAsync(userId, code);

    return RedirectToAction("Index");
}

这篇关于.NET身份电子邮件/用户名更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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