Reputation: 13
I created two IdentityDbContext, one is AdminIdentityDbContext:IdentityDbContext, and another is StudentIdentityDbContext:IdentityDbContext.
And I created two controllers, one is:
public class AdminAccountController : Controller
{
private UserManager<Admin> _userManager;
private SignInManager<Admin> _signInManager;
public AdminAccountController(UserManager<Admin> userManager, SignInManager<Admin> signInManager)
{
_userManager = userManager;
_signInManager = signInManager;
}
Another is:
public class StudentAccountController : Controller
{
private UserManager<Student> _userManager;
private SignInManager<Student> _signInManager;
public StudentAccountController(UserManager<Student> studentManager, SignInManager<Student> signInManager)
{
_userManager = studentManager;
_signInManager = signInManager;
}
My Startup.cs is as below:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using LibraryDemo.Data;
using LibraryDemo.Infrastructure;
using LibraryDemo.Models.DomainModels;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
namespace LibraryDemo
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddDbContext<LendingInfoDbContext>(options =>
{
options.UseSqlServer(Configuration.GetConnectionString("LendingInfoDbContext"));
});
services.AddDbContext<AdminIdentityDbContext>(options =>
{
options.UseSqlServer(Configuration.GetConnectionString("AdminIdentityDbContext"));
});
services.AddDbContext<StudentIdentityDbContext>(options =>
{
options.UseSqlServer(Configuration.GetConnectionString("StudentIdentityDbContext"));
});
services.AddIdentity<Student, IdentityRole>(opts =>
{
opts.User.RequireUniqueEmail = true;
opts.User.AllowedUserNameCharacters = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789";
opts.Password.RequiredLength = 6;
opts.Password.RequireNonAlphanumeric = false;
opts.Password.RequireLowercase = false;
opts.Password.RequireUppercase = false;
opts.Password.RequireDigit = false;
}).AddEntityFrameworkStores<StudentIdentityDbContext>()
.AddDefaultTokenProviders();
services.ConfigureApplicationCookie(opts =>
{
opts.LoginPath = "/StudentAccount/Login";
});
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseAuthentication();
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
DatabaseInitiator.Initial(app.ApplicationServices).Wait();
}
}
}
But when I got the site "/AdminAccount/Login", I got an error:
InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Identity.UserManager`1[LibraryDemo.Models.DomainModels.Admin]' while attempting to activate 'LibraryDemo.Controllers.AdminAccountController'.
I don't like the Role feature, so what else can I do to resolve this problem? Thanks a lot.
I tried to change the schema of the DbContext but failed the same.
Additional context I have put the project to github: https://github.com/NanaseRuri/LibraryDemo
Upvotes: 1
Views: 768
Reputation: 14472
Your issue is not caused by your two DB contexts, it's because of the way you're setting up the Identity middleware.
You're setting up your Identity middleware with the Student
and IdentityRole
classes. This means that the UserManager
is set up with the Student
class, and therefore there's no instance of a UserManager<Admin>
with the Admin
class.
Based on your structure, it seems that what you need are two roles: one for students, and the other for admins. That way, you can keep a single user manager.
Upvotes: 1