简体   繁体   English

ASP.NET身份编辑是否记录在用户的数据中?

[英]ASP.NET Identity editing logged in user's data?

I have modified the Views/Manage/Index.cshtml to display the User's email as well. 我修改了Views / Manage / Index.cshtml以显示用户的电子邮件。 I've modified the IndexViewModel as well so it recognizes the "Email" string and then made another .cshtml page similar to the changing of phone number one which is there by default. 我也修改了IndexViewModel,因此它识别出“Email”字符串,然后创建了另一个.cshtml页面,类似于默认情况下更改的电话号码。 The new page is called ChangeEmail.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>
}

From what I've seen, the changing of password happens through a task called "ChangePasswordAsync" inside UserManager.cs Is there a way to change the Email without making a new task? 从我所看到的,更改密码是通过UserManager.cs中名为“ChangePasswordAsync”的任务发生的。有没有办法在不进行新任务的情况下更改电子邮件?

EDIT: added more from the controller(index) : 编辑:从控制器(索引)添加更多:

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

Get the user's Email address from your ChangeEmailViewModel and then update the user's details using the userManager.UpdateAsync(user) ChangeEmailViewModel获取用户的电子邮件地址,然后使用userManager.UpdateAsync(user)更新用户的详细信息

EDIT 编辑

[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 });
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM