Seed database for Identity 2

前端 未结 4 1662
不知归路
不知归路 2020-12-08 11:20

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 Migration

相关标签:
4条回答
  • 2020-12-08 11:58

    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();
            }
        }
    
    0 讨论(0)
  • 2020-12-08 12:00

    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);

    0 讨论(0)
  • 2020-12-08 12:02

    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");
    }
    
    0 讨论(0)
  • 2020-12-08 12:08

    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);   
    ...
    
    0 讨论(0)
提交回复
热议问题