简体   繁体   中英

Can't inject UserManager dependency in Abp controller

I'm customizing IdentityServer4 UI to use Angular, I created SecurityController with the constructor:

    public SecurityController(
        MyDbContext dbContext, 
        UserManager<IdentityUser> userManager,
        AbpSignInManager signInManager
    )
    {
        _dbContext = dbContext;
        _userManager = userManager;
        _signInManager = signInManager;
    }

I'm getting the following exception when I refer to localhost:[port]/Security/[any action] :

DependencyResolutionException: None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'Acme.[ProjectName].Controllers.SecurityController' can be invoked with the available services and parameters: Cannot resolve parameter 'Microsoft.AspNetCore.Identity.UserManager 1[Microsoft.AspNetCore.Identity.IdentityUser] userManager' of constructor 'Void .ctor(Acme.[ProjectName].EntityFrameworkCore.MyDbContext, Microsoft.AspNetCore.Identity.UserManager 1[Microsoft.AspNetCore.Identity.IdentityUser], Volo.Abp.Identity.AspNetCore.AbpSignInManager)'.

PS If I remove UserManager dependency it works.

Any Idea?

EDIT: Here's my Startup.cs :

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddApplication<MyIdentityServerModule>();

        services.AddControllers();
    }

    public void Configure(IApplicationBuilder app)
    {
        app.InitializeApplication();

        app.UseStatusCodePagesWithRedirects("~/index.html");
    }
}

And here's MyIdentityServerModule :

    public override void OnApplicationInitialization(ApplicationInitializationContext context)
    {
        var app = context.GetApplicationBuilder();
        var env = context.GetEnvironment();

        ...

        app.UseRouting();
        ...
        app.UseAuthentication();

        ...

        app.UseIdentityServer();
        app.UseAuthorization();
        ...
    }

I'm not familiar with IdentityServer , but the UserManager class is part of the ASP.NET Identity middleware, and must be registered:

public void ConfigureServices(IServiceCollection services)
{
    services.AddIdentity<IdentityUser, IdentityRole>()
        .AddEntityFrameworkStores<ApplicationDbContext>()
        .AddDefaultTokenProviders();
}

You can check the IdentityServer quickstart guide for ASP.NET Core for more information.

我会在 UseIdentityServer() 之前删除 UseAuthentication(),因为 UseIdentityServer 为您添加了它。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM