Identity 2.0 Reset password by Admin
Asked Answered
D

2

7

How can I reset password as a admin for other users?

I have tried using the code below

var code = await UserManager.GeneratePasswordResetTokenAsync(user.Id);
var result = await UserManager.ResetPasswordAsync(user.Id, code, vm.NewPassword);

when stepping through GeneratePasswordResetTokenAsync, the dispose method of the controller is called.

Can someone please enlighten me?

Depopulate answered 21/5, 2014 at 12:31 Comment(0)
S
8

You can also extend UserManager and expose an explicit AdminChangePassword API that doesn't require any information. Something like this in ApplicationUserManager which extends UserManager should work:

public IdentityResult ChangePasswordAdmin(string userId, string newPassword) {
     var user = FindById(userId);
     // validate password using PasswordValidator.Validate
     user.PasswordHash = PasswordHasher.HashPassword(newPassword);
     Update(user);
}
Seasoning answered 13/6, 2014 at 18:29 Comment(0)
D
-1

I should have searched more before posting question here.

Apparently I need to wire up UserTokenProvider with a DataProtectorTokenProvider.

However I do not understand what's a DataProtector for, would be glad if someone can explain here.

var dataProtectionProvider = options.DataProtectionProvider;
if (dataProtectionProvider != null)
{
    manager.UserTokenProvider = new DataProtectorTokenProvider<MyUser>(dataProtectionProvider.Create("ASP.NET Identity"));
}

Answer is found here

Depopulate answered 21/5, 2014 at 12:38 Comment(2)
what does options refer to?Archaeo
` public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context){ var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<DBModel>())); var dataProtectionProvider = options.DataProtectionProvider; if (dataProtectionProvider != null) { manager.UserTokenProvider = new DataProtectorTokenProvider<MyUser>(dataProtectionProvider.Create("ASP.NET Identity")); } return manager; } `Oxtail

© 2022 - 2024 — McMap. All rights reserved.