c# - 启动应用程序时没有“Microsoft.AspNetCore.Identity.UserManager”类型的服务
问题描述
我有一个位于名为 App.ClassLibrary 的 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>
但它不会编译。
解决方案
推荐阅读
- javascript - 在我的打字稿文件中正确引用 (javascript) jquery 插件
- visual-studio - Jenkins Ms Build Error dot net core application
- python - Python保存到映射的共享驱动器错误
- sql - 如果结果中有多个条目,则计数为 1?
- html - Can't get rotated text aligned in Bootstrap 4 Grid
- appium - 如何验证本机应用链接并返回应用
- java - 如何将字段的名称应用于 ArrayList,然后向该 ArrayList 添加一些内容
- sql - Oracle SQL:检查条目是否存在,如果不存在则返回
- vba - 将 ComboBox 配置为只读:有办法吗?
- memory-leaks - 卡夫卡内存泄漏