Can't inject UserManager dependency in Abp controller

1.1k views Asked by At

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.UserManager1[Microsoft.AspNetCore.Identity.IdentityUser] userManager' of constructor 'Void .ctor(Acme.[ProjectName].EntityFrameworkCore.MyDbContext, Microsoft.AspNetCore.Identity.UserManager1[Microsoft.AspNetCore.Identity.IdentityUser], Volo.Abp.Identity.AspNetCore.AbpSignInManager)'.

P.S. 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();
        ...
    }
2

There are 2 answers

2
Métoule On

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.

0
Tore Nestenius On

I would remove UseAuthentication() before UseIdentityServer(), because UseIdentityServer adds it for you.