了解MVC-5的身份 [英] Understanding MVC-5 Identity

查看:179
本文介绍了了解MVC-5的身份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了单个用户帐户一个新的 ASP.NET MVC-5 应用,然后更新所有的的NuGet包在溶液中。现在我试图按照一些在一些教程所示的准则,但我遇到了一些问题。
第一个是叫做类 ApplicationRoleManager 正在被整个应用程序中使用未创建( ApplicationUserManager 是创建)。
第二个问题更多的是关于实体框架:我已经看到了与用户和角色很多人无欲无求的数据库中创建在 ApplicationDbContext 类:

I created a new ASP.NET MVC-5 application with Individual User Accounts and then updated all the Nuget packages in the solution. Now I'm trying to follow some of the guidelines shown in some tutorials but I encountered some problems. The first one is that a class called ApplicationRoleManager which is being used throughout the application wasn't created (the ApplicationUserManager was created). The second problem is more about Entity-Framework: I've seen that for seeding the database with a user and role many people create a static constructor in the ApplicationDbContext class:

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

加入的一切后,我打开了软件包管理器控制台键,输入启用的迁移,那么添加迁移someName 然后更新,数据库
结果是,该数据库被成功创建但没有数据被插入到数据库中。结果,
注意到该数据未插入后,我来到了种子逻辑到户控制器的索引方法和数据运行的应用程序后插入。
我还需要添加此行: app.CreatePerOwinContext&LT; ApplicationRoleManager&GT;(ApplicationRoleManager.Create);
Startup.Auth.cs 文件。结果
所以我的问题是:

After adding everything I opened the Package Manager Console and typed Enable-Migrations, then Add-Migration someName and then Update-Database. the results were that the database was created successfully but no data was inserted to the database.
After noticing the data wasn't inserted I moved the Seed logic to the Index method of the home controller and the data was inserted after running the application. I also needed to add this line: app.CreatePerOwinContext<ApplicationRoleManager>(ApplicationRoleManager.Create); to the Startup.Auth.cs file.
So my questions are:


  1. 请我真的需要进入 ApplicationRoleManager
    手动?

  2. 如何让种子方法的工作?

  1. Do I really need to enter the ApplicationRoleManager class manually?
  2. How do I make the seed method work?

更新结果
我已经改变了种子方法:

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, 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);
        }
        base.Seed(context);
    }

所以,现在,在创建管理员角色,但要获得 VAR用户= userManager.FindByName(名称)时; 应用程序挂起,没有例外或任何消息......

So now, the Admin role is created but when getting to var user = userManager.FindByName(name); the application hangs with no exception or any message...

推荐答案

在使用迁移您可以使用内置的初始化和种子方法:

When using migrations you can use the built in initializer and the Seed method:

Database.SetInitializer<ApplicationDbContext>(new 
    MigrateDatabaseToLatestVersion<ApplicationDbContext, 
    APPLICATION.Migrations.Configuration>());

APPLICATION.Migrations.Configuration (这是由启用的迁移命令创建):

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

作为一名角色管理器,你也可以使用 RoleManager&LT; ApplicationRole方式&gt; 基本实现

这篇关于了解MVC-5的身份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆