有人知道如何让用户通过ASP更改用户名/邮箱吗.网络身份与邮箱确认?有很多关于如何更改密码的例子,但我找不到任何关于这个的.

推荐答案

Update Dec 2017 comments 中提出了一些好的观点:

  • 在确认新邮件时,最好为其设置一个单独的字段,以防用户输入错误的邮箱.等待新邮件确认,然后将其作为主要邮件.请参见下面Chris_u的详细回答.
  • 此外,当该邮箱的帐户已经存在时,也可能会出现这种情况-确保您也判断了这一点,否则可能会有麻烦.

这是一个非常基本的解决方案,没有涵盖所有可能的组合,所以使用您的判断力,并确保您通读了 comments -其中提出了非常好的观点.

// 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");
}

Asp.net相关问答推荐

无法在Microsoft Windows Server 2016(数据中心)上运行.NET可移植性分析器

如何在 asp.net 服务器中使用字体

如何从 Azure 上托管的应用服务获取登录用户名?

如何使用 executeReader() 方法仅检索一个单元格的值

系统日期时间?与 System.DateTime

威胁已经被清除了

ASP.NET 在更新面板更新时显示正在加载...消息

Web.config 导致被组策略阻止错误

ASP.NET 中的 <% %>(嵌入式代码块)

如何在asp.net mvc的html.actionlink中调用javascript函数?

使用 ASPNet_Regiis 加密自定义配置部分 - 你能做到吗?

asp.net 有控制台日志(log)吗?

如何删除asp.net中的特定会话?

无法在 IIS 中启动网站 - W3SVC 正在运行

ASP.NET 5、EF 7 和 SQLite - SQLite 错误 1:没有这样的表:博客

Application_Start 和 Application_OnStart 之间的区别

ASP.Net 哪个用户帐户在 IIS 7 上运行 Web 服务?

Unity 中的单例每个调用上下文(Web 请求)

ASP.NET Web 窗体中的 jQuery 验证插件

如何在不遍历每个循环的情况下从字典对象中获取所有键(仅键)