112

I'm getting this error in Login Controller.

InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Identity.UserManager`1[Automobile.Models.Account]' while attempting to activate 'Automobile.Server.Controllers.AuthController'.

here is Auth Controller constructor:

private SignInManager<Automobile.Models.Account> _signManager;
    private UserManager<Automobile.Models.Account> _userManager;

    public AuthController(UserManager<Models.Account> userManager,
                          SignInManager<Automobile.Models.Account> signManager)
    {
        this._userManager = userManager;
        this._signManager = signManager;
    }

and here is ConfigureServices in startup.cs:

public void ConfigureServices(IServiceCollection services)
    {
        // Add framework services.
        services.AddApplicationInsightsTelemetry(Configuration);
        services.Configure<AppConfig>(Configuration.GetSection("AppSettings"));

        //var provider = HttpContext.ApplicationServices;
        //var someService = provider.GetService(typeof(ISomeService));


        services.AddDbContext<Providers.Database.EFProvider.DataContext>(options => options
            .UseSqlServer(Configuration.GetConnectionString("DefaultConnection"),
                 b => b.MigrationsAssembly("Automobile.Server")
            ));


        services.AddIdentity<IdentityUser, IdentityRole>(options =>
        {
            options.User.RequireUniqueEmail = false;
        })
        .AddEntityFrameworkStores<Providers.Database.EFProvider.DataContext>()
        .AddDefaultTokenProviders(); 
        //services.AddScoped<SignInManager<Automobile.Models.Account>, SignInManager<Automobile.Models.Account>>();
        //services.AddScoped<UserManager<Automobile.Models.Account>, UserManager<Automobile.Models.Account>>();

        services.AddMvc();
        App.Service = services.BuildServiceProvider();

        // Adds a default in-memory implementation of IDistributedCache.
        services.AddDistributedMemoryCache();

        services.AddSession(options =>
        {
            // Set a short timeout for easy testing.
            options.IdleTimeout = TimeSpan.FromSeconds(10);
            options.CookieHttpOnly = true;
        });

    }
OMID
  • 1,839
  • 4
  • 13
  • 23

7 Answers7

104

You need to use the same user data model in SignInManager, UserManager and services.AddIdentity. Same principal is true if you are using your own custom application role model class.

So, change

services.AddIdentity<IdentityUser, IdentityRole>(options =>
    {
        options.User.RequireUniqueEmail = false;
    })
    .AddEntityFrameworkStores<Providers.Database.EFProvider.DataContext>()
    .AddDefaultTokenProviders();

to

services.AddIdentity<Automobile.Models.Account, IdentityRole>(options =>
    {
        options.User.RequireUniqueEmail = false;
    })
    .AddEntityFrameworkStores<Providers.Database.EFProvider.DataContext>()
    .AddDefaultTokenProviders();
HojjatK
  • 1,310
  • 1
  • 13
  • 16
  • 2
    I have the similar problem, I have my customer user and role defined in the AddIdentity method and I'm still getting the same error. Any idea why? I could post my code in a separate thread. – devC Jan 07 '18 at 16:30
  • 1
    Actually, I resolved that issue, but now facing a problem with creating the DB connection. I'll post the problem. – devC Jan 09 '18 at 04:20
  • https://stackoverflow.com/questions/48161467/database-connection-not-created-in-identitydbcontext – devC Jan 09 '18 at 04:31
  • seems your connection string has been not set correctly, check out my answer in your post. – HojjatK Jan 11 '18 at 11:17
52

Just to be clear about the answer:

If you use the class ApplicationUser in startup.cs: services.AddIdentity<ApplicationUser, IdentityRole>()

then you must use the same class in your controller when injecting it:

public AccountController(UserManager<ApplicationUser> userManager)

If you use some other class such as:

public AccountController(UserManager<IdentityUser> userManager)

then you will get this error:

InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Identity.UserManager`1[IdentityUser]'

because you used ApplicationUser in startup, not IdentityUser so this type is not registered with the injection system.

Erik Philips
  • 48,663
  • 7
  • 112
  • 142
Greg Gum
  • 25,941
  • 27
  • 127
  • 194
  • 6
    This counts for all references, so if you implement the new Razor identity for asp.net core 2.1 to replace your old identitysystem, you need to replace their automatic implementation of stuff like SignInManager to SignInManager wherever it is used. This can be annoying. Also you need to re-route from normal /Account/Login to /Identity/Account/Login etc. just some tips to help ;) – Johan Herstad Jun 04 '18 at 07:02
19

This is a bit unrelated to the original post but since Google brings you here... if you are getting this error and are using:

services.AddIdentityCore<YourAppUser>()

Then you will need to manually register the stuff that AddIdentity does, which can be found here: https://github.com/aspnet/Identity/blob/feedcb5c53444f716ef5121d3add56e11c7b71e5/src/Identity/IdentityServiceCollectionExtensions.cs#L79

        services.AddHttpContextAccessor();
        // Identity services
        services.TryAddScoped<IUserValidator<TUser>, UserValidator<TUser>>();
        services.TryAddScoped<IPasswordValidator<TUser>, PasswordValidator<TUser>>();
        services.TryAddScoped<IPasswordHasher<TUser>, PasswordHasher<TUser>>();
        services.TryAddScoped<ILookupNormalizer, UpperInvariantLookupNormalizer>();
        services.TryAddScoped<IRoleValidator<TRole>, RoleValidator<TRole>>();
        // No interface for the error describer so we can add errors without rev'ing the interface
        services.TryAddScoped<IdentityErrorDescriber>();
        services.TryAddScoped<ISecurityStampValidator, SecurityStampValidator<TUser>>();
        services.TryAddScoped<ITwoFactorSecurityStampValidator, TwoFactorSecurityStampValidator<TUser>>();
        services.TryAddScoped<IUserClaimsPrincipalFactory<TUser>, UserClaimsPrincipalFactory<TUser, TRole>>();
        services.TryAddScoped<UserManager<TUser>>();
        services.TryAddScoped<SignInManager<TUser>>();
        services.TryAddScoped<RoleManager<TRole>>();

You'll need to replace TUser and TRole with your implementations of those, or the default IdentityUser, IdentityRole

Serj Sagan
  • 24,625
  • 17
  • 133
  • 166
  • Had to create a custom SignInManager and this fixed it, if you plan on doing so check https://github.com/dotnet/docs/issues/14828 – perustaja Jun 22 '20 at 04:46
  • I originally started down this route but found the solution here (https://stackoverflow.com/a/60752194/1146862) more straight forward; The only change I had to make (after re-ordering `AddIdentity` and `AddJwtBearer` was to set all three options shown in the example; I'd only been using `DefaultAuthenticationScheme`. I still get the cookie back on login, but `[Authorize]` now works for the JWT tokens without specifying an AuthenticationSchema. – Aaron Sep 20 '20 at 11:19
  • 1
    This is exactly what I was missing. Adding `TryAddScoped>();` and `services.TryAddScoped>();` to my Startup.cs fixed my issue. – MorganR Dec 22 '20 at 14:07
6

don't forget to add role manager in ConfigureServices

services.AddDefaultIdentity<IdentityUser>()
    .AddRoles<IdentityRole>() // <--------
    .AddDefaultUI(UIFramework.Bootstrap4)
    .AddEntityFrameworkStores<ApplicationDbContext>();
Ozzy
  • 192
  • 1
  • 12
4

You can set IdentityUser and IdentityRole in ConfigureServices inside Startup class individually as shown below:

services.AddDefaultIdentity<IdentityUser>()
    .AddRoles<IdentityRole>()
    .AddEntityFrameworkStores<ApplicationDbContext>();

OR

you can configure directly into AddIdentity:

services.AddIdentity<IdentityUser, IdentityRole>()
        .AddEntityFrameworkStores<ApplicationDbContext>();
0

If you're using "IdentityServer" so IdentityServer authenticates the user and authorizes the client.By default, IdentityServer is actually not about user management. But there is some support for asp.net Identity

So you need add :

services.AddIdentityServer()
    .AddAspNetIdentity<ApplicationUser>();
Amirhossein Yari
  • 1,652
  • 3
  • 20
  • 31
0

You need to update your Statup.cs class with below

services.AddIdentity<ApplicationUser,IdentityRole>() .AddEntityFrameworkStores();

Here : ApplicationUser is my custom model class.

Gaurav Joshi
  • 353
  • 2
  • 9