c# - ASP.NET 身份编辑登录用户数据?

标签 c# asp.net asp.net-mvc razor asp.net-identity

我修改了 Views/Manage/Index.cshtml 以显示用户的电子邮件。我也修改了 IndexViewModel,因此它可以识别“Email”字符串,然后创建另一个 .cshtml 页面,类似于默认情况下更改电话号码一号。新页面称为 ChangeEmail.cshtml

@using (Html.BeginForm("ChangeEmail", "Manage", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
    @Html.AntiForgeryToken()
    <h4>Add an email</h4>
    <hr />
    @Html.ValidationSummary("", new { @class = "text-danger" })
    <div class="form-group">
        @Html.LabelFor(model => model.Email, new { @class = "col-md-2 control-label" })
        <div class="col-md-10">
            @Html.TextBoxFor(model => model.Email, new { @class = "form-control" })

        </div>
    </div>
    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" class="btn btn-default" value="Submit" />
        </div>
    </div>
}

据我所知,密码的更改是通过 UserManager.cs 中名为“ChangePasswordAsync”的任务进行的 有没有办法在不创建新任务的情况下更改电子邮件?

编辑:从 Controller (索引)添加更多内容:

    public async Task<ActionResult> Index(ManageMessageId? message)
            {
                ViewBag.StatusMessage =
                    message == ManageMessageId.ChangePasswordSuccess ? "Your password has been changed."
                    : message == ManageMessageId.SetPasswordSuccess ? "Your password has been set."
                    : message == ManageMessageId.SetTwoFactorSuccess ? "Your two-factor authentication provider has been set."
                    : message == ManageMessageId.Error ? "An error has occurred."
                    : message == ManageMessageId.AddPhoneSuccess ? "Your phone number was added."
                    : message == ManageMessageId.RemovePhoneSuccess ? "Your phone number was removed."
                    : message == ManageMessageId.EmailChangedSuccess ? "Your email has been changed"
                    : "";

                var userId = User.Identity.GetUserId();
                var userEmail = User.Identity.Name;
                var user = UserManager.FindById(userId);



                var model = new IndexViewModel
                {
                    HasPassword = HasPassword(),
                    PhoneNumber = await UserManager.GetPhoneNumberAsync(userId),
                    TwoFactor = await UserManager.GetTwoFactorEnabledAsync(userId),
                    Logins = await UserManager.GetLoginsAsync(userId),
                    BrowserRemembered = await AuthenticationManager.TwoFactorBrowserRememberedAsync(userId),
                    Email = user.Email,
                    City = user.City,
                    Region = user.Region


                };
                user.Email = "topkek@ucn.dk";
                UserManager.UpdateAsync(user);

                return View(model);
            }

 [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> ChangeEmail(ChangeEmailViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return View(model);
            }



            return RedirectToAction("Index", new { Message = ManageMessageId.EmailChangedSuccess });
        }

最佳答案

从您的 ChangeEmailViewModel 获取用户的电子邮件地址,然后使用 userManager.UpdateAsync(user) 更新用户的详细信息

编辑

[HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> ChangeEmail(ChangeEmailViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return View(model);
            }
             //Get the user's Id
            var userId = User.Identity.GetUserId(); 

            //get the user (you can modify the variables to fit yours) 
           var user = UserManager.FindByIdAsync(userId);

          //this is how to change the Email
          user.Result.Email= model.Email// **EDIT**;

         userManager.UpdateAync(user.Result);

            return RedirectToAction("Index", new { Message = ManageMessageId.EmailChangedSuccess });
        }

关于c# - ASP.NET 身份编辑登录用户数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36800757/

相关文章:

c# - 为什么 IIS 会在一段时间后停止?

c# - 带有依赖注入(inject)的 Telerik Reporting ObjectDataSource

.net - 将自定义 ValueProviderFactories 添加到 ASP.NET MVC3?

c# - 如何仅在 Release模式下在 web.config 中设置属性?

c# - 动态创建字段的 Tabindex

c# - GridView 没有拾取空数据对象

c# - 提供程序配置的自定义配置文件

asp.net-mvc - ASP.NET MVC 4 - 用户组的唯一路由?

c# - 将空行缩短为一行 c# Regex

c# - ColorAnimation 为滑动时的 listviewItem 颜色设置动画 - WP8.1