2014-09-21 44 views
4

我想通過MVC5爲代碼優先遷移和OWIN標識2授予用戶和角色種子。升級到2.0之前,我有這個工作。如何通過使用OWIN標識2和MVC5代碼優先進行種子用戶和角色

首先,從IdentityConfig.cs文件中使用非代碼第一遷移例如當DropCreateDatabaseIfModelChanges

的ApplicationDbInitializer級以下工作:

public class ApplicationDbInitializer : DropCreateDatabaseIfModelChanges<ApplicationDbContext> 
{ 
    protected override void Seed(ApplicationDbContext context) 
    { 
     InitializeIdentityForEF(context); 
     base.Seed(context); 
    } 


    public static void InitializeIdentityForEF(ApplicationDbContext db) 
    { 
     var userManager = HttpContext 
      .Current.GetOwinContext() 
      .GetUserManager<ApplicationUserManager>(); 

     var roleManager = HttpContext.Current 
      .GetOwinContext() 
      .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); 
     } 
    } 
} 

我感動InitializeIdentityForEF代碼到我的configuration.cs文件併成功添加了這兩個引用和項目構建。

using Microsoft.AspNet.Identity.Owin; 
using Microsoft.AspNet.Identity; 

,但我得到以下錯誤,當我從包管理器控制檯運行更新的數據庫:

System.NullReferenceException:對象不設置到對象的實例。 在System.Web.HttpContextExtensions.GetOwinEnvironment(HttpContext的背景下) 在System.Web.HttpContextExtensions.GetOwinContext(HttpContext的背景下)

更新

每OdeToCode評論,你不能使用OWIN包管理器控制檯: 看看第51行這裏:raw.githubusercontent.com/OdeToCode/MVC5_Samples/master/...我不認爲你會在Seed方法中使用Owin成功,除非你在應用程序內運行Seed。如果您從包管理器控制檯運行Seed,Owin將不在或未配置。 - OdeToCode 5月13日15:00

solution有希望,但是在構建具有以下錯誤:

錯誤4類型「Foo.Models。 ApplicationUser'不能用作通用類型或方法'Microsoft.AspNet.Identity.EntityFramework.UserStore'中的類型參數'TUser'。沒有從'Foo.Models.ApplicationUser'到'Microsoft.AspNet.Identity.EntityFramework.IdentityUser'的隱式引用轉換。 C:\ FOO \遷移\ Configuration.cs 86 43

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

所以,你解決了你的問題? – trailmax 2014-09-21 19:09:52

+1

我遇到同樣的問題。你找到解決方案嗎? – 2014-10-28 22:42:21

回答

0

從亡羊補牢版... 更好的晚間新聞,如果你有

var userManager = new ApplicationUserManager(new UserStore(context));

一切替換

var userStore = new UserStore<ApplicationUser>(context); 
var userManager = new UserManager<ApplicationUser>(userStore); 

應該管用。我注意到

0

在我來說,什麼工作在這裏給出的思路:

http://www.codeproject.com/Articles/674760/Code-First-Migration-and-Extending-Identity-Accoun

我取代這段代碼在種方法:

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

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

這一個:

 var conText = new ApplicationDbContext(); 
     var userStore = new UserStore<ApplicationUser>(conText); 
     var userManager = new UserManager<ApplicationUser>(userStore); 

     var roleStore = new RoleStore<IdentityRole>(conText); 
     var roleManager = new RoleManager<IdentityRole>(roleStore); 
相關問題