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. >我真的需要进入ApplicationRoleManager类 UPDATE 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>基础实施. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |