How to seed a user into the database with ASP.NET Identity?

asp.net asp.net-identity-3 asp.net-mvc c# entity-framework-6

Question

For security, I don't want just anybody to be able to register on my site but I can't stop users from registering while there is a registration page accessible on the site, so I added an "Authorized" flag to the ApplicationUser model:

public class ApplicationUser : IdentityUser
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public bool Authorized { get; set; }
}

The idea is that if a user, whether logged in or not, does not have the value for this property set to true, the user will not be able to access secured content. A previously authorized user will have to authorize new users.

This brings into question how to get the first user authorized so I figured I'd seed the user in on my Migration. See below:

namespace Ortund.Migrations
{
    internal sealed class Configuration :
        DbMigrationsConfiguration<Ortund.Models.ApplicationDbContext>
    {
        protected override void Seed(Ortund.Models.ApplicationDbContext context)
        {
            context.Users.AddOrUpdate(
                u => u.Id,
                new Models.ApplicationUser { EmailAddress = "email@site.com", Authorized = true, EmailConfirmed = true }
            );
        }
    }
}

Obviously I'm missing some crucial fields here Password hashes for one and that's where I'm having trouble.

I don't know how the UserManager hashes the password so I can't seem replicate that process here since the function is not an async Task.

For reference, here's the Register method in the AccountController:

public async Task<ActionResult> Register(RegisterViewModel model)
{
    if (ModelState.IsValid)
    {
        var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
        var result = await UserManager.CreateAsync(user, model.Password);
        ...
    }
}

The CreateAsync function specifies a cleartext password string that will be hashed. Attempting to replicate this in my Seed method above, I only have access to ApplicationUserManager.Create(Microsoft.AspNet.Identity.Owin.IdentityFactorOptions<ApplicationUserManager> options, Microsoft.Owin.IOwinContext context) which as far as I can see, doesn't suit my requirement here.

How can I create a full ApplicationUser in my seeded data?

1
0
3/7/2017 9:44:16 AM

Popular Answer

You could call in to the UserManager.Create method and pass it the username and plain text password. That will hash the password for you and get that into the database

Something like this:

var userStore = new UserStore<ApplicationUser>(context);
var userManager = new UserManager<ApplicationUser>(userStore);
var user = new ApplicationUser { UserName = "username"};
userManager.Create(user, "password");
5
3/7/2017 9:55:39 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow