Multiple Identity in ASP.net Core

To implement multiple identity users in ASP.NET Core MVC (e.g., having separate authentication for different types of users like admin, customer, etc.), follow these steps:

1. Create Multiple User Models

You will need separate models for different types of users if they have distinct properties. For example:

public class AdminUser : IdentityUser
{
    public string AdminSpecificProperty { get; set; }
}

public class CustomerUser : IdentityUser
{
    public string CustomerSpecificProperty { get; set; }
}

2. Create Separate DbContexts for Each User Type

You can create separate DbContext classes to handle different user types.

public class AdminDbContext : IdentityDbContext<AdminUser>
{
    public AdminDbContext(DbContextOptions<AdminDbContext> options) : base(options) { }
}

public class CustomerDbContext : IdentityDbContext<CustomerUser>
{
    public CustomerDbContext(DbContextOptions<CustomerDbContext> options) : base(options) { }
}

3. Configure Identity for Each User Type in Program.cs (or Startup.cs)

Register each DbContext and configure the identity options:

builder.Services.AddDbContext<AdminDbContext>(options =>
    options.UseSqlServer(builder.Configuration.GetConnectionString("AdminConnection")));

builder.Services.AddDbContext<CustomerDbContext>(options =>
    options.UseSqlServer(builder.Configuration.GetConnectionString("CustomerConnection")));

builder.Services.AddIdentity<AdminUser, IdentityRole>()
    .AddEntityFrameworkStores<AdminDbContext>();

builder.Services.AddIdentity<CustomerUser, IdentityRole>()
    .AddEntityFrameworkStores<CustomerDbContext>();

// Authentication Schemes
builder.Services.AddAuthentication()
    .AddCookie("AdminScheme", options =>
    {
        options.LoginPath = "/Admin/Account/Login";
        options.AccessDeniedPath = "/Admin/Account/AccessDenied";
    })
    .AddCookie("CustomerScheme", options =>
    {
        options.LoginPath = "/Customer/Account/Login";
        options.AccessDeniedPath = "/Customer/Account/AccessDenied";
    });

4. Use Authentication Schemes in Controllers

For different types of users, apply authentication schemes in the corresponding controllers:

[Authorize(AuthenticationSchemes = "AdminScheme")]
public class AdminController : Controller
{
    // Admin actions
}

[Authorize(AuthenticationSchemes = "CustomerScheme")]
public class CustomerController : Controller
{
    // Customer actions
}

5. Login and Logout for Different User Types

In your login action, you can specify the authentication scheme:

public async Task<IActionResult> AdminLogin(string returnUrl = null)
{
    var result = await _signInManager.PasswordSignInAsync(userName, password, false, false);
    if (result.Succeeded)
    {
        return RedirectToAction("Index", "Admin");
    }
    return View();
}

public async Task<IActionResult> CustomerLogin(string returnUrl = null)
{
    var result = await _signInManager.PasswordSignInAsync(userName, password, false, false);
    if (result.Succeeded)
    {
        return RedirectToAction("Index", "Customer");
    }
    return View();
}