2014-05-09 68 views
14

我遇到了使用Identity v2種子數據庫的問題。我將MVC5項目中的IdentityModel分離到我的數據訪問層,並在其中設置EF Migrations。所以我註釋掉這裏面使用「IdentityConfig.cs」創建初始用戶,並把代碼看起來像這樣Identity 2的種子數據庫

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 = "[email protected]"; 
      const string password = "[email protected]"; 
      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); 
      } 
     } 

現在,當我運行命令更新數據庫我的種子庫中的代碼,我得到了一個錯誤

Value cannot be null. 
Parameter name: manager 

那樣子,我的代碼

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

任何建議,請這兩行越來越空?

+4

看線51位置:https://raw.githubusercontent.com/OdeToCode/MVC5_Samples/master/identity/BasicIdentityWithDiagrams /Migrations/Configuration.cs我不認爲你會在Seed方法中使用Owin成功,除非你在應用程序內運行Seed。如果您從包管理器控制檯運行Seed,Owin將不在或未配置。 – OdeToCode

回答

25

這是爲了避免使用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"); 
} 
1

我用得到這個工作:

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

喜在啓動類請確保您有來電 應用.CreatePerOwinContext(ApplicationDbContext.Create); app.CreatePerOwinContextApplicationUserManager.Create); app.CreatePerOwinContextApplicationSignInManager.Create);

app.CreatePerOwinContext(ApplicationRoleManager.Create);

0

最新的東西是所有異步&使用索賠。 這裏是爲我工作與遷移,如果不存在添加一個超級用戶...

protected override void Seed(Holos.Service.Models.ApplicationDbContext context) 
    { 
     var email  = "[email protected]"; 
     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(); 
     } 
    }