112

新しい ASP.NET Identity システムでユーザーのパスワードを取得するにはどうすればよいですか? または、現在のパスワードを知らずにリセットするにはどうすればよいですか (ユーザーがパスワードを忘れた場合)。

4

10 に答える 10

163

Or how can I reset without knowing the current one (user forgot password)?

If you want to change a password using the UserManager but you do not want to supply the user's current password, you can generate a password reset token and then use it immediately instead.

string resetToken = await UserManager.GeneratePasswordResetTokenAsync(model.Id);
IdentityResult passwordChangeResult = await UserManager.ResetPasswordAsync(model.Id, resetToken, model.NewPassword);
于 2015-03-24T15:23:31.710 に答える
110

現在のリリースでは

忘れたパスワードをリセットする要求の検証を処理したと仮定して、次のコードをサンプル コードの手順として使用します。

ApplicationDbContext =new ApplicationDbContext()
String userId = "<YourLogicAssignsRequestedUserId>";
String newPassword = "<PasswordAsTypedByUser>";
ApplicationUser cUser = UserManager.FindById(userId);
String hashedNewPassword = UserManager.PasswordHasher.HashPassword(newPassword);
UserStore<ApplicationUser> store = new UserStore<ApplicationUser>();            
store.SetPasswordHashAsync(cUser, hashedNewPassword);

AspNet ナイトリー ビルドで

フレームワークは、ForgetPassword などの要求を処理するために Token と連携するように更新されています。リリースされると、簡単なコード ガイダンスが期待されます。

アップデート:

この更新は、より明確な手順を提供するためのものです。

ApplicationDbContext context = new ApplicationDbContext();
UserStore<ApplicationUser> store = new UserStore<ApplicationUser>(context);
UserManager<ApplicationUser> UserManager = new UserManager<ApplicationUser>(store);
String userId = User.Identity.GetUserId();//"<YourLogicAssignsRequestedUserId>";
String newPassword = "test@123"; //"<PasswordAsTypedByUser>";
String hashedNewPassword = UserManager.PasswordHasher.HashPassword(newPassword);                    
ApplicationUser cUser = await store.FindByIdAsync(userId);
await store.SetPasswordHashAsync(cUser, hashedNewPassword);
await store.UpdateAsync(cUser);
于 2013-10-22T19:29:13.827 に答える
71

非推奨

これが元の答えでした。動作しますが、問題があります。AddPassword失敗したら?ユーザーはパスワードなしで残されます。

元の答え: 3 行のコードを使用できます。

UserManager<IdentityUser> userManager = 
    new UserManager<IdentityUser>(new UserStore<IdentityUser>());

userManager.RemovePassword(userId);

userManager.AddPassword(userId, newPassword);

参照: http://msdn.microsoft.com/en-us/library/dn457095(v=vs.111).aspx

今おすすめ

EdwardBreyが提案し、その後DanielWrightがコード サンプルで詳しく説明した回答を使用することをお勧めします。

于 2014-03-18T20:53:12.043 に答える
30

で、まずGeneratePasswordResetTokenAsync をUserManager呼び出します。ユーザーが自分の身元を確認したら (たとえば、電子メールでトークンを受け取ることによって)、そのトークンをResetPasswordAsyncに渡します。

于 2014-04-15T20:49:12.897 に答える