1

I know that we can initialise the database when doing a Entity Framework migration using the Seed method, as below.

        protected override void Seed(CraigSheppardSoftware.Models.ApplicationDbContext context)
    {
        context.Users.AddOrUpdate(p => p.UserName, 
            new ApplicationUser { FullName = "System Admin", UserName="CssOp", UserRole = UserType.SystemAdmin, 
                LandlinePhone=new ComplexDataTypes.PhoneNumber(), MobilePhone= new ComplexDataTypes.PhoneNumber()
            } );
        context.SaveChanges();
    }

The problem is that the when I try to use that user to login with, I can't as I dont know what the password is.

enter image description here

There is no password field on the database. I notice that there is a PasswordHash field and suspect that this is an encrypted password. How do I create a password hash ?

SystemsGuy
  • 153
  • 10

1 Answers1

3

Use UserManager to create / update the user.

var manager = new UserManager<ApplicationUser>(
    new UserStore<ApplicationUser>(context));
var user = manager.Find("CssOp", "ThePassword");
if (user == null)
{
    user = new ApplicationUser { UserName = "CssOp", Email = "email@mail.com" };
    manager.Create(user, "ThePassword");
}
else
{
    user.Email = "newemail@mail.com";
    manager.Update(user);
}
Yuliam Chandra
  • 14,494
  • 12
  • 52
  • 67