簡體   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