2017-09-14 31 views
0

所以我做了一个后台管理仪表板,我想为当前用户添加一项功能,以便能够在他们登录时更改他们的密码。是我的Account Settings页面的相关代码,其中正在发生更改密码。 用户信息全部存储在一个名为LoginDataModel的模型数据库中我只是无法解决我的生活如何让当前登录用户能够更改他/她自己的密码,然后将其登出能够使用新密码登录?我甚至尝试过使用UserManagement CRUD应用程序中的编辑部分,但仍然没有任何结果。改变当前登录的用户密码[MVC]

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

希望能帮到你!

+0

您使用的是ASP.NET Identity吗?或者其他一些认证/授权提供商? (我是你自己创建的,你可能会留下很多安全漏洞:不这样做。) – Richard

+0

@Richard我已经在我的UserManagementController的使用区域中获得了它,但是我不知道它是否已设置或配置正常。我是整个MVC的新手 –

回答

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