繁体   English   中英

更改当前登录的用户密码[MVC]

[英]Changing current logged in user password [MVC]

因此,我正在制作一个后端管理仪表板,我想为当前用户添加一项功能,以便他们在登录时能够更改其密码。以下是我与“ Account Settings页相关的代码,密码将发生。 用户信息全部存储在一个名为LoginDataModel的模型数据库中,我一生都LoginDataModel如何获取当前登录用户以更改其自己的密码,然后将其注销以便能够用新密码登录? 我什至尝试使用UserManagement CRUD应用程序中的Edit(编辑)部分,但是还是没有。

public class UserManagementController : Controller
{
    private UserDatabaseEntities db = new UserDatabaseEntities();


    public ActionResult AccountSettings(int? id)
    {
        if (id == null)
        {
            return View();
        }
        Login login = db.Logins.Find(id);
        if (login == null)
        {
            return HttpNotFound();
        }
        return View(login);
    }

    [HttpPost]
    public ActionResult AccountSettings([Bind(Include = "Password")]Login login)
    {
        if (ModelState.IsValid)
        {
            User.Identity.GetUserId();
            db.Entry(login).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        return View(login);
    }
}


@using (Html.BeginForm())
{
    <div class="container col-sm-4 col-md-4 col-lg-4">
        <div class="form-group">
            <label for="AssemblyName">New Password :</label>
            <div class="form-group" style="width: 300px;">
                <div>
                    @Html.EditorFor(model => model.Password, new { htmlAttributes = new { @class = "form-control", @placeholder = "New Password" } })
                    @Html.ValidationMessageFor(model => model.Password, "", new { @class = "text-danger" })
                </div>
            </div>
        </div>
        <button type="submit" class="btn btn-info" style="width: 200px; "><span class="glyphicon glyphicon-plus-sign"></span>Save Changes</button>
        <a href="#" class="btn btn-danger" type="button" style="width: 100px;">Delete</a>
    </div>
}

希望能对您有所帮助!

If you are using asp identity and want to change password of user then first you need to create reset password token 

string userId = User.Identity.GetUserId();
string token = await UserManager.GeneratePasswordResetTokenAsync(userId);

And then use that token to reset password 

var result = await UserManager.ResetPasswordAsync(userId, token, newPassword);

暂无
暂无

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

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