如何使用 OWIN Identity 2 和 MVC5 通过代码优先迁移为用户和角色播种

Bar*_*SIH 6 owin asp.net-identity entity-framework-migrations

我正在尝试使用 MVC5 使用 Code First Migration 和 OWIN Identity 2 为用户和角色设定种子。在升级到 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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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

using Microsoft.AspNet.Identity.Owin;
using Microsoft.AspNet.Identity;
Run Code Online (Sandbox Code Playgroud)

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

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

更新

根据 OdeToCode 评论,您不能使用 OWIN 包管理器控制台:请查看此处的第 51 行:raw.githubusercontent.com/OdeToCode/MVC5_Samples/master/...我认为您不会在 Seed 方法中成功使用 Owin,除非您是从应用程序内部运行种子。如果您从包管理器控制台运行 Seed,则 Owin 将不会出现或配置。– OdeToCode 5 月 13 日 15:00

解决方案有希望,但构建有以下错误:

错误 4 类型 'Foo.Models. ApplicationUser '不能用作泛型类型或方法'Microsoft.AspNet.Identity.EntityFramework.UserStore'中的类型参数'TUser'。没有从“Foo.Models.ApplicationUser”到“M​​icrosoft.AspNet.Identity.EntityFramework.IdentityUser”的隐式引用转换。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");
}
Run Code Online (Sandbox Code Playgroud)

小智 0

迟到总比不到好版本的新闻...如果您更换

var userStore = new UserStore<ApplicationUser>(context);
var userManager = new UserManager<ApplicationUser>(userStore);
Run Code Online (Sandbox Code Playgroud)

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

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