15

I came across a problem for seeding the database with Identity v2. I separated out the IdentityModel from the MVC5 project to my Data Access Layer where I setup EF Migrations as well. So I commented out the code which use inside "IdentityConfig.cs" to create initial user and put the code inside my seed database that looks like this

  protected override void Seed(Repository.DataContext.IdentityDb context)
        {

            //    var userManager = HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>();
            //    var roleManager = HttpContext.Current.GetOwinContext().Get<ApplicationRoleManager>();
            var owinContext = new OwinContext();
            var userManager = owinContext.GetUserManager<ApplicationUserManager>();
            var roleManager = owinContext.Get<ApplicationRoleManager>();
            const string name = "admin@admin.com";
            const string password = "Admin@123456";
            const string roleName = "Admin";

            //    //Create Role Admin if it does not exist
            var role = roleManager.FindByName(roleName);
            if (role == null)
            {
                role = new IdentityRole(roleName);
                var roleresult = roleManager.Create(role);
            }

            var user = userManager.FindByName(name);
            if (user == null)
            {
                user = new ApplicationUser { UserName = name, Email = name };
                var result = userManager.Create(user, password);
                result = userManager.SetLockoutEnabled(user.Id, false);
            }

            //    // Add user admin to Role Admin if not already added
            var rolesForUser = userManager.GetRoles(user.Id);
            if (!rolesForUser.Contains(role.Name))
            {
                var result = userManager.AddToRole(user.Id, role.Name);
            }
        }  

Now when I am running command update-database, I got an error

Value cannot be null.
Parameter name: manager

It looks like, I am getting null in these two lines of code

var userManager = owinContext.GetUserManager<ApplicationUserManager>();
var roleManager = owinContext.Get<ApplicationRoleManager>();

Any suggestion please?

Ammar Khan
  • 2,565
  • 6
  • 35
  • 60
  • 4
    Look at line 51 here: https://raw.githubusercontent.com/OdeToCode/MVC5_Samples/master/identity/BasicIdentityWithDiagrams/Migrations/Configuration.cs I don't think you'll have success using Owin in the Seed method unless you are running Seed from inside the app. Owin won't be around or configured if you run Seed from the package manager console. – OdeToCode May 13 '14 at 15:00

4 Answers4

28

This is the way to avoid using an OWIN context:

protected override void Seed(Repository.DataContext.IdentityDb context)
    var roleStore = new RoleStore<IdentityRole>(context);
    var roleManager = new RoleManager<IdentityRole>(roleStore);
    var userStore = new UserStore<ApplicationUser>(context);
    var userManager = new UserManager<ApplicationUser>(userStore);               
    var user = new ApplicationUser { UserName = "sallen" };

    userManager.Create(user, "password");                    
    roleManager.Create(new IdentityRole { Name = "admin" });
    userManager.AddToRole(user.Id, "admin");
}
Rob Church
  • 6,783
  • 3
  • 41
  • 46
user3311522
  • 1,638
  • 3
  • 19
  • 33
2

I got this working by using:

protected override void Seed(ApplicationDbContext context)
        {
            context.Configuration.LazyLoadingEnabled = true;

            //var userManager = HttpContext.Current
            //    .GetOwinContext().GetUserManager<ApplicationUserManager>();

            //var roleManager = HttpContext.Current
            //    .GetOwinContext().Get<ApplicationRoleManager>();

            var roleStore = new RoleStore<ApplicationRole, int, ApplicationUserRole>(context);
            var roleManager = new RoleManager<ApplicationRole, int>(roleStore);
            var userStore = new UserStore<ApplicationUser, ApplicationRole, int, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>(context);
            var userManager = new UserManager<ApplicationUser, int>(userStore);   
...
goobering
  • 1,547
  • 2
  • 10
  • 24
0

Hi Under the Startup class please make sure that you have call app.CreatePerOwinContext(ApplicationDbContext.Create); app.CreatePerOwinContextApplicationUserManager.Create); app.CreatePerOwinContextApplicationSignInManager.Create);

app.CreatePerOwinContext(ApplicationRoleManager.Create);

Ahsanul
  • 215
  • 2
  • 5
0

Latest stuff is all async & uses Claims. Here's what worked for me with migrations to add a super user if none exists ...

    protected override void Seed(Holos.Service.Models.ApplicationDbContext context)
    {
        var email       = "xxxx@xxxx.com";
        var password    = "xxxxx";
        var userStore   = new UserStore<ApplicationUser>(context);
        var userManager = new ApplicationUserManager(userStore);

        var user = userManager.FindByEmailAsync(email).Result;
        if (user == null)
        {
            var adminUser = new ApplicationUser() { Email = email, UserName = email };
            var result = userManager.CreateAsync(adminUser, password);
            result.Wait();
            userManager.AddClaimAsync(adminUser.Id, new Claim("Read", "*")).Wait();
            userManager.AddClaimAsync(adminUser.Id, new Claim("Create", "*")).Wait();
            userManager.AddClaimAsync(adminUser.Id, new Claim("Update", "*")).Wait();
            userManager.AddClaimAsync(adminUser.Id, new Claim("Delete", "*")).Wait();
            userManager.AddClaimAsync(adminUser.Id, new Claim("UserType", "SuperUser")).Wait();
        }
    }
silverfox1948
  • 857
  • 10
  • 15