Understanding the MVC-5 Identifier

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 fulfill some of the recommendations shown in some tutorials, but I ran into some problems. The first is that the ApplicationRoleManager class, which is used throughout the application, was not created ( ApplicationUserManager was created). The second problem is more about Entity-Framework : I saw that for planting a database using a user and a role, many people create a static constructor in the ApplicationDbContext class:

  static ApplicationDbContext() { Database.SetInitializer<ApplicationDbContext>(new ApplicationDbInitializer()); } 

So, I added it, and the implementation of 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); } } 

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 the data was not inserted into the database.
After you noticed that the data was not inserted, I moved the Seed logic to the Index method of the home controller, and the data was inserted after the application started. I also needed to add this line: app.CreatePerOwinContext<ApplicationRoleManager>(ApplicationRoleManager.Create); to the file Startup.Auth.cs .
So my questions are:

  • Do I need to enter the ApplicationRoleManager class manually?
  • How to create a Seed method?

UPDATE
I changed the Seed method to:

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

So, now the administrator role is created, but when accessing var user = userManager.FindByName(name); the application freezes without any exception or any message ...

+6
source share
7 answers

When using migration, you can use the built-in initializer and the Seed method:

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

and in APPLICATION.Migrations.Configuration (this was created by the Enable-Migrations command):

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

As a role manager, you can also use the basic implementation of RoleManager<ApplicationRole> .

+6
source

In this case, I was also a little embarrassed about the suspension of the application. Thus, the problem can be solved.

 var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUserManager>(db)); var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(db)); 
+3
source

And for those using ApplicationUser with the Integer internal key, the code is as follows:

 var userManager = new ApplicationUserManager(new ApplicationUserStore(context)); var roleManager = new ApplicationRoleManager(new ApplicationRoleStore(context)); 
+2
source

This works fine for the MVC 5 project by default.

 var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context)); 
+1
source

It seems that the solutions did not address the problem of the application userManager.FindByName(name) when calling userManager.FindByName(name) . I am facing the same problem. He worked a few hours ago at my local. I posted to Azure and it started to hang. When I tested my locator again, he suddenly started hanging out at this step. The error is not returned and there is no timeout (at least after waiting 10-15 minutes). Does anyone have any advice on resolving Yoav's final question?

I have other very simple seeding processes that are performed before adding roles, and the db.Foo.AddOrUpdate(foo) calls work without errors, but actually save nothing in the database.

0
source

I spent a very unpleasant half day on this. I finally managed to get this damn thing:

 public static void InitializeIdentityForEF(ApplicationDbContext context) { context.Configuration.LazyLoadingEnabled = true; //var userManager = HttpContext.Current // .GetOwinContext().GetUserManager<ApplicationUserManager>(); //var roleManager = HttpContext.Current // .GetOwinContext().Get<ApplicationRoleManager>(); var roleStore = new RoleStore<ApplicationRole, int, ApplicationUserRole>(context); var roleManager = new RoleManager<ApplicationRole, int>(roleStore); var userStore = new UserStore<ApplicationUser, ApplicationRole, int, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>(context); var userManager = new UserManager<ApplicationUser, int>(userStore); ... 

This is the end of a very long day, and I suspect that someone will tell me why I should not do this. The rest of my Seed method works fine, however, using non-asynchronous methods (FindByName / Create).

0
source

Sir Goberding, you did your best to solve this problem, I had to do it a little differently.

  context.Configuration.LazyLoadingEnabled = true; //var userManager = HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>(); //var roleManager = HttpContext.Current.GetOwinContext().Get<ApplicationRoleManager>(); const string name = " admin@example.com "; const string password = " Admin@123456 "; const string roleName = "Admin"; ***var userManager = new ApplicationUserManager(new UserStore<ApplicationUser>(context)); var roleManager = new ApplicationRoleManager(new RoleStore<IdentityRole>(context));*** //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); } 
0
source

Source: https://habr.com/ru/post/973320/


All Articles