如何使用带有MVC5的OWIN Identity 2进行代码优先迁移的用户和角色种子

时间:2014-09-21 13:19:11

标签: asp.net-identity owin ef-migrations

我正在尝试使用Code First Migration和OWIN Identity 2与MVC5为用户和角色提供种子。我在升级到2.0之前就已经开始工作了。

首先,在使用DropCreateDatabaseIfModelChanges

的非代码优先迁移示例时,以下工作正常

IdentityConfig.cs文件中的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 = "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);
        }
    }
}

我将InitializeIdentityForEF代码移动到我的configuration.cs文件中,并成功添加了这两个引用和项目构建。

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

但是当我从软件包管理器控制台运行update-database时出现以下错误:

System.NullReferenceException:未将对象引用设置为对象的实例。    在System.Web.HttpContextExtensions.GetOwinEnvironment(HttpContext context)    在System.Web.HttpContextExtensions.GetOwinContext(HttpContext context)

更新

根据OdeToCode注释,您无法使用OWIN Package Manager控制台: 请看这里的第51行:raw.githubusercontent.com/OdeToCode/MVC5_Samples/master/...除非您在应用程序内部运行Seed,否则我认为您在Seed方法中使用Owin会取得成功。如果从包管理器控制台运行Seed,Owin将无法使用或配置。 - OdeToCode 5月13日15:00

solution有承诺,但构建有以下错误:

错误4类型&#39; Foo.Models。 ApplicationUser &#39;不能用作类型参数&#39; TUser&#39;在通用类型或方法&#39; Microsoft.AspNet.Identity.EntityFramework.UserStore&#39;。来自&#39; Foo.Models.ApplicationUser&#39;没有隐式引用转换。到&#39; Microsoft.AspNet.Identity.EntityFramework.IdentityUser&#39;。 c:\ foo \ Migrations \ 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");
}

2 个答案:

答案 0 :(得分:0)

迟到的消息比从来没有版本好...... 如果你替换

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

  

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

一切都应该有效。我注意到了

答案 1 :(得分:0)

就我而言,这里提出的想法是有效的:

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

我在Seed方法中替换了这段代码:

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