No password Hasing MVC Identity 2.0

I know that all passwords must be hashed on a DB and I do it in all my personal projects; but unfortunately some times the customers ask me to have the clear users password.
That’s why in some company the users are very newbie and they the missed password to the IT manager.
Here the right code to achieve that:

Create a file NoPasswordHasher.cs in your project

class NoPasswordHasher : IPasswordHasher
{
    public string HashPassword(string password)
    {
        return password;
    }
    
    public PasswordVerificationResult VerifyHashedPassword(string hashedPassword, string providedPassword)
    {
        return (hashedPassword == providedPassword) ? PasswordVerificationResult.Success : PasswordVerificationResult.Failed;
    }
}

go to App_Start\IdentityConfig.cs and

// Configure the application user manager used in this application. UserManager is defined in ASP.NET Identity and is used by the application.
public class ApplicationUserManager : UserManager<ApplicationUser>
{
    public ApplicationUserManager(IUserStore<ApplicationUser> store)
        : base(store)
    {
    }

    public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context) 
    {
        var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>()));

        // ADD THIS LINE to configure our new password hasher
        manager.PasswordHasher = new NoPasswordHasher();

        manager.UserValidator = new UserValidator<ApplicationUser>(manager)
            {
                AllowOnlyAlphanumericUserNames = false,
                RequireUniqueEmail = true
            };
        ...
    }
}
Annunci

Informazioni su Andrea Regoli

Project Manager .Net Developer WPF WP7 Asp.Net c# javascript ajax SQL sharepoint
Questa voce è stata pubblicata in Asp.Net, MVC e contrassegnata con , , , . Contrassegna il permalink.

Rispondi

Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:

Logo WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione / Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione / Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione / Modifica )

Google+ photo

Stai commentando usando il tuo account Google+. Chiudi sessione / Modifica )

Connessione a %s...