The new ASP.net Identity project has brought some useful code and interfaces for website security. To implement a custom system using the interfaces (instead of using the standard Entity Framework implementation included in the MVC 5 template) an IPasswordHasher
is required.
IPasswordHasher
interface in ASP.net Identity
namespace Microsoft.AspNet.Identity
{
public interface IPasswordHasher
{
string HashPassword(string password);
PasswordVerificationResult VerifyHashedPassword(string hashedPassword, string providedPassword);
}
}
Is it possible to use password salting for more secure encryption in ASP.net Identity and via this interface?
Yes, the interface is provided for the new implementation of PasswordHasher already present in Core framework.
Also note that the default implementation is already using Salt+Bytes.
After creating custom
PasswordHasher
(sayMyPasswordHasher
), you can assign it to UserManager instance likeuserManager.PasswordHasher=new MyPasswordHasher()
See one example of such IPasswordHasher
For implementing alternate system from EF, - You shall implement all Core interfaces. - IPasswordHasher implementation is not required. PasswordHasher is already provided in Core framework as it's implementation.
HEALTH WARNING for the below answer: Know which version of ASP.Net Identity you are using. You should refer to the source code directly if it is one of the newer versions from the github repository.
As I write this, the current version (3.0.0-rc1/.../PasswordHasher.cs) of the password handler is significantly different to the below answer. This newer version supports multiple hash algorithm versions and is documented as (and may change further by the time you read this):
The original answer is still valid for the original version of ASP.Net Identity, and is as follows:
@jd4u is correct, but to shed a little more light which wouldn't fit into a comment for his answer:
Microsoft.AspNet.Identity.PasswordHasher : IPasswordHasher
already salts for you,Rfc2898DeriveBytes
to generate the salt and the hash,Microsoft.AspNet.Identity.UserManager<TUser>
implementation usesMicrosoft.AspNet.Identity.PasswordHasher
as a concreteIPasswordHasher
PasswordHasher
in turn is a really simple wrapper for (ultimately)System.Security.Cryptography.Rfc2898DeriveBytes
So, if you are going to use
Rfc2898DeriveBytes
, just usePasswordHasher
- all the heavy lifting is already done (hopefully correctly) for you.Details
The full code that PasswordHasher (currently) ultimately uses does something very close to:
I ran into an issue while updating from Membership to AspNet.Identity. The Rfc2898 hashes are different from those used before. That's for good reason, but changing the hashes would require all users to reset their passwords. As a solution this custom implementation makes it backwards compatible:
Once you create your UserManager instance just set the hasher:
The code complains that the
HashPasswordForStoringInConfigFile
method is deprecated, but that's fine as we know that the whole exercise is to get rid of the old technology.