2014-05-21 4 views
6

Come posso reimpostare la password come amministratore per altri utenti?Identity 2.0 Reimposta password da Admin

Ho provato usando il codice seguente

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

quando si passa tramite GeneratePasswordResetTokenAsync, il metodo smaltire il regolatore viene richiamato.

Qualcuno può per favore illuminarmi?

+0

Che versione di mvc? –

+0

mvc 5.1, ha aggiornato il tag. grazie –

risposta

-1

Avrei dovuto cercare di più prima di pubblicare la domanda qui.

A quanto pare ho bisogno di collegare UserTokenProvider con un DataProtectorTokenProvider.

Tuttavia, non capisco cosa sia un DataProtector per, sarebbe contento se qualcuno può spiegare qui.

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

risposta si trova here

+0

a cosa si riferiscono le "opzioni"? – Colum

+0

' ApplicationUserManager public static Crea (IdentityFactoryOptions Opzioni, contesto IOwinContext) {var manager = new ApplicationUserManager (nuova UserStore (context.Get ())); var dataProtectionProvider = options.DataProtectionProvider; se (dataProtectionProvider! = Null) { manager.UserTokenProvider = nuovo DataProtectorTokenProvider (dataProtectionProvider.Create ("ASP.NET Identity")); } gestore di ritorno; } ' – DotNetGeek

7

è anche possibile estendere UserManager ed esporre un'API AdminChangePassword esplicita che non richiede alcuna informazione. Qualcosa di simile in ApplicationUserManager che estende UserManager dovrebbe funzionare:

public IdentityResult ChangePasswordAdmin(string userId, string newPassword) { 
    var user = FindById(userId); 
    // validate password using PasswordValidator.Validate 
    user.PasswordHash = PasswordHasher.HashPassword(newPassword); 
    Update(user); 
}