更改现有Asp.Net MVC 5(身份2)申请种子的方法? [英] Change seed method for an existing Asp.Net MVC 5 (Identity 2) application?

查看:164
本文介绍了更改现有Asp.Net MVC 5(身份2)申请种子的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何添加一个固定的角​​色(例如管理员)到现有的Asp.Net的mvc /身份2个网站?

在连结
<一href=\"http://bitoftech.net/2015/03/11/asp-net-identity-2-1-roles-based-authorization-authentication-asp-net-web-api/\" rel=\"nofollow\">http://bitoftech.net/2015/03/11/asp-net-identity-2-1-roles-based-authorization-authentication-asp-net-web-api/,有下列code这正是我想做的事情。但是,将种子函数,因为应用程序执行的? (我已经有一些code, VAR lookup1 =新的List&LT; L1&GT; {新的L1 {...},...}; tableL1.ForEach(L =&GT; context.tableL1。 AddOrUpdate(p =&GT; p.Title,1)); context.SaveChanges(); 种子函数种子一些查找表)。什么是角色添加到应用程序这已经是网上的正确方法?

  //内部密封类配置:DbMigrationsConfiguration&LT; ApplicationDbContext&GT;
内部密封类配置:DbMigrationsConfiguration&LT; ApplicationDbContext&GT;
{
    公共配置()
    {
        AutomaticMigrationsEnabled = FALSE;
    }    保护覆盖无效的种子(AspNetIdentity.WebApi.Infrastructure.ApplicationDbContext上下文)
    {
        //该方法将迁移到最新版本之后被调用。        VAR经理=新的UserManager&LT; ApplicationUser&GT;(新UserStore&LT; ApplicationUser&GT;(新ApplicationDbContext()));        变种roleManager =新RoleManager&所述; IdentityRole&GT;(新Rolestore的&所述; IdentityRole&GT;(新ApplicationDbContext()));        VAR用户=新ApplicationUser()
        {
            用户名=SuperPowerUser
            电子邮件=taiseer.joudeh@gmail.com
            EmailConfirmed = TRUE,
            名字=Taiseer
            姓氏=Joudeh
            级别= 1,
            JoinDate = DateTime.Now.AddYears(-3)
        };        manager.Create(用户,MySuperP @ SS!);        如果(roleManager.Roles.Count()== 0)
        {
            roleManager.Create(新IdentityRole {名称=超级管理员});
            roleManager.Create(新IdentityRole {名称=管理});
            roleManager.Create(新IdentityRole {名称=用户});
        }        VAR ADMINUSER = manager.FindByName(SuperPowerUser);        manager.AddToRoles(adminUser.Id,新的String [] {超级管理员,管理});
    }
}


解决方案

如果您希望种子您对数据库启用的迁移包管理器控制台

我猜你已经做了已经,因为你已经在 Configuration.cs 文件。

在你的构造函数配置类确保 AutomaticMigrationsEnabled 设置为或你的数据库将被重新创建。

 公共配置()
{
    AutomaticMigrationsEnabled = FALSE;
}

现在你可以编译你的解决方案,并从软件包管理器控制台(请确保您选择在默认项目组合合适的项目)运行:

 更新 - 数据库

 更新,数据库-Verbose

如果你想收集更多的信息。

,你应该看到在数据库中的新数据。

How to add a fixed role (e.g. Admin) to an existing Asp.Net Mvc/Identity 2 site?

In the link http://bitoftech.net/2015/03/11/asp-net-identity-2-1-roles-based-authorization-authentication-asp-net-web-api/, there are the following code which is exactly what I want to do. However, will the Seed function be executed since the application? (I already have some code, var lookup1 = new List<L1>{new L1 {...},...}; tableL1.ForEach(l => context.tableL1.AddOrUpdate(p=>p.Title, l)); context.SaveChanges();, in the Seed function to seed some lookup tables.). What's the right way to Add a role to an application which is already online?

// internal sealed class Configuration : DbMigrationsConfiguration<ApplicationDbContext>
internal sealed class Configuration : DbMigrationsConfiguration<ApplicationDbContext>
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = false;
    }

    protected override void Seed(AspNetIdentity.WebApi.Infrastructure.ApplicationDbContext context)
    {
        //  This method will be called after migrating to the latest version.

        var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));

        var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(new ApplicationDbContext()));

        var user = new ApplicationUser()
        {
            UserName = "SuperPowerUser",
            Email = "taiseer.joudeh@gmail.com",
            EmailConfirmed = true,
            FirstName = "Taiseer",
            LastName = "Joudeh",
            Level = 1,
            JoinDate = DateTime.Now.AddYears(-3)
        };

        manager.Create(user, "MySuperP@ss!");

        if (roleManager.Roles.Count() == 0)
        {
            roleManager.Create(new IdentityRole { Name = "SuperAdmin" });
            roleManager.Create(new IdentityRole { Name = "Admin"});
            roleManager.Create(new IdentityRole { Name = "User"});
        }

        var adminUser = manager.FindByName("SuperPowerUser");

        manager.AddToRoles(adminUser.Id, new string[] { "SuperAdmin", "Admin" });
    }
}

解决方案

If you want to seed the database you have to Enable-Migrations in the Package Manager Console.

I guess you have done that already since you already have the Configuration.cs file.

In the constructor of you configuration class make sure AutomaticMigrationsEnabled is set to false or your database will be recreated.

public Configuration()
{
    AutomaticMigrationsEnabled = false;
}

Now you can compile your solution and from Package Manager Console (Make sure your selecting the right project in the Default Project combo) run:

Update-Database

or

Update-Database -Verbose

if you want to collect more info.

and you should see your new data in the database.

这篇关于更改现有Asp.Net MVC 5(身份2)申请种子的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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