When using multiple identity users, you need to make sure that the correct UserManager
is injected into the controller or service. This can be achieved by specifying the user type when registering the UserManager
in the Startup.cs
file.
Here is an example of how to register two UserManager
instances for two different user types in the ConfigureServices
method:
services.AddIdentity<User1, IdentityRole>() .AddEntityFrameworkStores<ApplicationDbContext>() .AddDefaultTokenProviders(); services.AddScoped<UserManager<User1>, UserManager<User1>>(); services.AddIdentity<User2, IdentityRole>() .AddEntityFrameworkStores<ApplicationDbContext>() .AddDefaultTokenProviders(); services.AddScoped<UserManager<User2>, UserManager<User2>>();
In this example, two UserManager
instances are registered, one for User1
and another for User2
. You can then inject the correct UserManager
into the controller or service using the appropriate generic type parameter:
public class MyController : Controller { private readonly UserManager<User1> _userManager1; private readonly UserManager<User2> _userManager2; public MyController(UserManager<User1> userManager1, UserManager<User2> userManager2) { _userManager1 = userManager1; _userManager2 = userManager2; } // Controller methods }
"Multiple Identity Users with UserManager in ASP.NET Core"
// Startup.cs - ConfigureServices method services.AddIdentity<User1, Role>().AddEntityFrameworkStores<ApplicationDbContext>(); services.AddIdentity<User2, Role>().AddEntityFrameworkStores<ApplicationDbContext>();
"Customize Identity UserManager for multiple users"
// ApplicationUserManager.cs - Customizing UserManager for User1 public class ApplicationUserManager1 : UserManager<User1> { // Customizations for UserManager } // ApplicationUserManager2.cs - Customizing UserManager for User2 public class ApplicationUserManager2 : UserManager<User2> { // Customizations for UserManager } // Startup.cs - ConfigureServices method services.AddScoped<UserManager<User1>, ApplicationUserManager1>(); services.AddScoped<UserManager<User2>, ApplicationUserManager2>();
"ASP.NET Core Identity with multiple UserManagers"
// MyController.cs - Constructor injection with UserManager for User1 public class MyController : Controller { private readonly UserManager<User1> _userManager; public MyController(UserManager<User1> userManager) { _userManager = userManager; } // Controller actions }
"Identity UserManager registration for multiple users"
// Startup.cs - ConfigureServices method services.AddIdentity<User1, Role>().AddEntityFrameworkStores<ApplicationDbContext>().AddDefaultTokenProviders(); services.AddIdentity<User2, Role>().AddEntityFrameworkStores<ApplicationDbContext>().AddDefaultTokenProviders();
"Scoped UserManager for multiple identity users"
// Startup.cs - ConfigureServices method services.AddScoped<UserManager<User1>>(); services.AddScoped<UserManager<User2>>();
"Identity User class with different UserManager"
// User1.cs - Identity user class User1 public class User1 : IdentityUser { // Properties for User1 } // Startup.cs - ConfigureServices method services.AddIdentity<User1, Role>().AddEntityFrameworkStores<ApplicationDbContext>(); services.AddScoped<UserManager<User1>>();
"Scoped service registration for UserManager in ASP.NET Core"
// Startup.cs - ConfigureServices method services.AddScoped<UserManager<User1>>(); services.AddScoped<UserManager<User2>>();
"Configure different Identity UserManagers in ASP.NET Core"
// MyService.cs - Constructor injection with UserManager for User2 public class MyService { private readonly UserManager<User2> _userManager; public MyService(UserManager<User2> userManager) { _userManager = userManager; } // Service logic }
"Identity UserManager dependency injection for multiple users"
// MyService.cs - Constructor injection with UserManager for User1 public class MyService { private readonly UserManager<User1> _userManager; public MyService(UserManager<User1> userManager) { _userManager = userManager; } // Service logic }
"Identity User classes with different UserManager registrations"
// Startup.cs - ConfigureServices method services.AddIdentity<User1, Role>().AddEntityFrameworkStores<ApplicationDbContext>(); services.AddIdentity<User2, Role>().AddEntityFrameworkStores<ApplicationDbContext>(); services.AddScoped<UserManager<User1>>(); services.AddScoped<UserManager<User2>>();
android-virtual-device movable photokit ear mkannotation command exoplayer2.x projection v-for qlistwidget