我有一个位于ApplicationLibrary的ClassLibrary中的ApplicationDbContext。这是我执行数据库迁移和模型创建的地方。
我还有另一个名为App.UI的应用程序,它依赖于App.ClassLibrary。但是,似乎当我启动应用程序时,我的LoginPartial给了我这个错误
InvalidOperationException:类型不提供服务 'Microsoft.AspNetCore.Identity.UserManager`1 [Microsoft.AspNetCore.Identity.IdentityUser]' 已注册。
这是我在App.UI中的启动
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString(DatabaseGlobals.ConnectionStringName)));
services.AddMvc();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString(DatabaseGlobals.ConnectionStringName)));
services.AddIdentity<Users,Roles>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
}
// 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();
app.UseDatabaseErrorPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseAuthentication();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
using (var serviceScope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>().CreateScope())
{
serviceScope.ServiceProvider.GetService<ApplicationDbContext>().Database.Migrate();
}
}
这是我的用户和角色类
public class Users : IdentityUser
{
public string FirstName { get; set; }
public string LastName { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime DateOfBirth { get; set; }
private DateTime createdOn = DateTime.Now;
public DateTime CreatedOn
{
get
{
return (createdOn == DateTime.MinValue) ? DateTime.Now : createdOn;
}
set
{
createdOn = value;
}
}
private DateTime updatedOn = DateTime.Now;
public DateTime UpdatedOn
{
get
{
return (updatedOn == DateTime.MinValue) ? DateTime.Now : updatedOn;
}
set
{
updatedOn = value;
}
}
}
public class Roles : IdentityRole
{
[Required]
public string ApplicationId { get; set; }
public virtual Applications Application { get; set; }
}
最后,这是LoginPartial页面,该页面给我错误:
@using Microsoft.AspNetCore.Identity
@inject SignInManager<IdentityUser> SignInManager
@inject UserManager<IdentityUser> UserManager
@if (SignInManager.IsSignedIn(User))
{
<form asp-area="Identity" asp-page="/Account/Logout" asp-route-returnUrl="@Url.Action("Index", "Home", new { area = "" })" method="post" id="logoutForm" class="navbar-right">
<ul class="nav navbar-nav navbar-right">
<li>
<a asp-area="Identity" asp-page="/Account/Manage/Index" title="Manage">Hello @UserManager.GetUserName(User)!</a>
</li>
<li>
<button type="submit" class="btn btn-link navbar-btn navbar-link">Logout</button>
</li>
</ul>
</form>
}
else
{
<ul class="nav navbar-nav navbar-right">
<li><a asp-area="Identity" asp-page="/Account/Register">Register</a></li>
<li><a asp-area="Identity" asp-page="/Account/Login">Login</a></li>
</ul>
}
我曾尝试将SignInManager从<IdentityUser>
切换到<Users>
,但无法编译。