15

我遇到了使用 Identity v2 为数据库播种的问题。我将 IdentityModel 从 MVC5 项目分离到我的数据访问层,在那里我也设置了 EF 迁移。所以我注释掉了在“IdentityConfig.cs”中使用的代码来创建初始用户并将代码放在我的种子数据库中,看起来像这样

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

现在,当我运行命令 update-database 时,出现错误

Value cannot be null.
Parameter name: manager

看起来,我在这两行代码中得到了 null

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

请问有什么建议吗?

4

4 回答 4

28

这是避免使用 OWIN 上下文的方法:

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");
}
于 2014-05-26T00:37:47.473 回答
2

我通过以下方式得到了这个工作:

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);   
...
于 2015-01-30T03:19:06.897 回答
0

您好在 Startup 类下,请确保您已调用 app.CreatePerOwinContext(ApplicationDbContext.Create); app.CreatePerOwinContextApplicationUserManager.Create); app.CreatePerOwinContextApplicationSignInManager.Create);

app.CreatePerOwinContext(ApplicationRoleManager.Create);

于 2015-02-02T17:59:42.947 回答
0

最新的东西都是异步的并使用声明。如果没有超级用户,这对我有用的迁移添加超级用户......

    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();
        }
    }
于 2016-04-06T16:49:37.417 回答