首页 > 解决方案 > 启动应用程序时没有“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>但它不会编译。

标签: c#asp.net-core

解决方案


推荐阅读