c# – 了解MVC-5身份

前端之家收集整理的这篇文章主要介绍了c# – 了解MVC-5身份前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我使用个人用户帐户创建了一个新的ASP.NET MVC-5应用程序,然后更新了解决方案中的所有Nuget包.现在我试图遵循一些教程中显示的一些指导原则,但我遇到了一些问题.
第一个是没有创建在整个应用程序中使用的名为ApplicationRoleManager的类(ApplicationUserManager已创建).
第二个问题更多的是关于Entity-Framework:我已经看到,为了用一个用户和角色为数据库播种,许多人在ApplicationDbContext类中创建一个静态构造函数
static ApplicationDbContext()
    {
        Database.SetInitializer<ApplicationDbContext>(new ApplicationDbInitializer());
    }

所以我添加了它,ApplicationDbInitializer的实现是:

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

    //Create User=Admin@Admin.com with password=Admin@123456 in the Admin role
    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);
        }
    }

添加完所有内容后,我打开了包管理器控制台并键入了Enable-Migrations,然后添加了迁移someName,然后是Update-Database.
结果是数据库已成功创建,但没有数据插入数据库.
注意到未插入数据后,我将Seed逻辑移动到主控制器的Index方法,并在运行应用程序后插入数据.
我还需要添加以下行:app.CreatePerOwinContext< ApplicationRoleManager>(ApplicationRoleManager.Create);
到Startup.Auth.cs文件.
所以我的问题是:

>我真的需要进入ApplicationRoleManager类
手动?
>如何使种子方法有效?

UPDATE
我已将Seed方法更改为:

protected override void Seed(ApplicationDbContext context)
    {
        var userManager = HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>();

        //since there is no ApplicationRoleManager (why is that?) this is how i create it
        var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context));

        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);
        }
        //app hangs here...
        var user = userManager.FindByName(name);
        if (user == null)
        {
            user = new ApplicationUser { UserName = name,role.Name);
        }
        base.Seed(context);
    }

所以现在,创建了Admin角色,但是在获取var user = userManager.FindByName(name)时;应用程序挂起,没有例外或任何消息……

解决方法

使用迁移时,您可以使用内置初始化程序和Seed方法
Database.SetInitializer<ApplicationDbContext>(new 
    MigrateDatabaseToLatestVersion<ApplicationDbContext,APPLICATION.Migrations.Configuration>());

在APPLICATION.Migrations.Configuration中(这是由Enable-Migrations命令创建的):

protected override void Seed(ApplicationDbContext context)
{
    // seed logic
}

作为角色管理器,您还可以使用RoleManager< ApplicationRole>基础实施.

原文链接:https://www.f2er.com/csharp/96370.html

猜你在找的C#相关文章