Estou tentando usar meu método seed
abaixo para criar um usuário no banco de dados quando ele é criado:
protected override void Seed(CodingCraftMod1Ex4AuthMembershipContext context)
{
string password = PasswordsHelper.EncodePassword("123456", System.Web.Security.MembershipPasswordFormat.Hashed);
var user = new CustomUser
{
CustomUserId = Guid.NewGuid(),
Name = "MyUser",
CreatedOn = DateTime.Now,
LastModified = DateTime.Now
};
context.CustomUsers.Add(user);
context.SaveChanges();
var membership = new Membership
{
MembershipId = Guid.NewGuid(),
CustomUser = user,
Password = password,
CreatedOn = DateTime.Now,
LastModified = DateTime.Now,
};
context.Memberships.Add(membership);
context.SaveChanges();
}
Mas recebo o seguinte erro:
Hashed or Encrypted passwords are not supported with auto-generated keys
Eu já estou usando o elemento machineKey
, assim:
<machineKey validationKey="13687AD58719815734D5ECA97AADA159F4084FE994E32192243818A714DD6BC763B9F3D8AE7B3A7858A268D8EAAB37BF5031E77E5971C82BC1ACEA478C76C6CF"
decryptionKey="A39F3B62B3CAAD3F75358197CA1D880BA3F392BE79AE4E91D2A09219D82A6978"
validation="SHA1"
decryption="AES" />
Eu usei essa ferramenta online para gerar as chaves.
Segue abaixo o trecho personalizado do método que faz o hash da senha no método EncodePassword
:
case MembershipPasswordFormat.Hashed:
HMACSHA1 hash = new HMACSHA1();
hash.Key = HexToByte(machineKey.ValidationKey);
encodedPassword =
Convert.ToBase64String(hash.ComputeHash(Encoding.Unicode.GetBytes(password)));
break;