2017-09-14 4 views
0

バックエンドの管理ダッシュボードを作成しています。現在ログインしているユーザーのパスワードを変更できるようにする機能を追加したいと考えています。パスワードが変更される場所のAccount Settingsページの関連コードです。 ユーザー情報はすべてLoginDataModelというモデルデータベースに保存されています。現在のログインユーザーが自分のパスワードを変更してログアウトする方法新しいパスワードでログインできますか?私はUserManagement CRUDアプリケーションのEdit部分を使ってみましたが、まだ何もしませんでした。現在ログインしているユーザーパスワードを変更する[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 IDを使用していますか?または他の認証/認可プロバイダですか? (あなたは自分で自分自身を作成し​​ていますが、セキュリティホールをたくさん残してしまうでしょう: – Richard

+0

@リチャード私は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); 
関連する問題