简体   繁体   中英

How to solve "Unable to resolve service for type 'Microsoft.AspNetCore.Identity.UserManager"?

MVC client: ASP.NET CORE 3 Identity (with scaffolded Identity). Identity Server 4 (v3).

Created by quickstart .

MVC client Startup has:

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

AddIdentity helps me register new user.

When I'm trying MVC client sign in with Identity Server, I get infinite loop. I read , that need to remove AddIdentity, because my Client connecting to Id Server. When I remove AddIdentity a login works perfectly.

But my MVC client has scaffolded Identity for User Registration. When I'm trying open User register form ( http://localhost:5002/Identity/Account/Register ), I get error:

InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Identity.UserManager`1[SharedIdentity.Models.ApplicationUser]' while attempting to activate 'UserRegistration.Areas.Identity.Pages.Account.RegisterModel'.

When I try to register without Identity UserManager:

 services.AddScoped<UserManager<ApplicationUser>>();
 services.AddScoped<SignInManager<ApplicationUser>>();

I get other error:

Unhandled exception. System.AggregateException: Some services are not able to be constructed 
(Error while validating the service descriptor 'ServiceType: 
Microsoft.AspNetCore.Identity.UserManager`1[SharedIdentity.Models.ApplicationUser] 
Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.UserManager`1[SharedIdentity.Models.ApplicationUser]': 
Unable to resolve service for type 'Microsoft.AspNetCore.Identity.IUserStore`1[SharedIdentity.Models.ApplicationUser]' while attempting to activate 'Microsoft.AspNetCore.Identity.UserManager`1[SharedIdentity.Models.ApplicationUser]'.) 
(Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Identity.SignInManager`1[SharedIdentity.Models.ApplicationUser] Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.SignInManager`1[SharedIdentity.Models.ApplicationUser]': Unable to resolve service for type 'Microsoft.AspNetCore.Identity.IUserStore`1[SharedIdentity.Models.ApplicationUser]' 
while attempting to activate 'Microsoft.AspNetCore.Identity.UserManager`1[SharedIdentity.Models.ApplicationUser]'.)
 ---> System.InvalidOperationException: Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Identity.UserManager`1[SharedIdentity.Models.ApplicationUser] Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.UserManager`1[SharedIdentity.Models.ApplicationUser]': 

Unable to resolve service for type 'Microsoft.AspNetCore.Identity.IUserStore`1[SharedIdentity.Models.ApplicationUser]' while attempting to activate 'Microsoft.AspNetCore.Identity.UserManager`1[SharedIdentity.Models.ApplicationUser]'.
 ---> System.InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Identity.IUserStore`1[SharedIdentity.Models.ApplicationUser]' while attempting to activate 'Microsoft.AspNetCore.Identity.UserManager`1[SharedIdentity.Models.ApplicationUser]'.

......
System.InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Identity.IUserStore`1[SharedIdentity.Models.ApplicationUser]' while attempting to activate 'Microsoft.AspNetCore.Identity.UserManager`1[SharedIdentity.Models.ApplicationUser]'.
   at

Maybe I'm on wrong way? I need, that my MVC client can register users, and can login users with Identity Server.

This doesn't answer the question as in " solve the problem ". But it does address the issue of setting up user management and it does seem to be an acceptable answer for @hdoitc:

First of all, a client shouldn't have access to the Identity model . This is the responsibility of IdentityServer. The same counts for login. The user doesn't login on the client website but on the IdentityServer website. As for registration, the user is registered in IdentityServer not in the client.

IdentityServer authenticates the user and authorizes the client. So you'll need to send the user to the IdentityServer website for login and registration. Consider a user as an entity that is not bound to one client. With its account it can use any client, unless authorization is denied, though IdentityServer is not recommended for user authorization:

PolicyServer is our recommendation for user authorization.

If you are interested, here's a link.

By default, there isn't much UI for Identity, because IdentityServer is actually not about user management. But there is some support for asp.net Identity .

You can add views anyway you like, as this is asp.net Identity. I suggest you create a new project and scaffold the Identity files as documented here and use whatever you need, or take a look at the sources here . If I'm not mistaken then there is UI for RazorPages, but not for Mvc.

You can improve the user experience in IdentityServer by customizing the views, eg add a logo, use a theme by identifying the client by the clientId.

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.

Related Question Unable to resolve service for type 'Microsoft.AspNetCore.Identity.UserManager Unable to resolve service for type 'Microsoft.AspNetCore.Identity.UserManager` while attempting to activate 'AuthController' Unable to resolve service for type 'Microsoft.AspNetCore.Identity.UserManager hile attempting to activate 'Controllers.AccountsController' Unable to resolve service for type 'Microsoft.AspNetCore.Identity.UserManager` while attempting to activate 'AuthenticateController' Unable to resolve service for type 'Microsoft.AspNetCore.Identity.UserManager' while attempting to activate 'Management.Controllers.RoleController' Unable to resolve service for type 'Microsoft.AspNetCore.Identity.UserManager' while attempting to activate 'WebShop.Controllers.User.UserController' No service for type 'Microsoft.AspNetCore.Identity.UserManager`1[Microsoft.AspNetCore.Identity.IdentityUser]' has been registered No service for type 'Microsoft.AspNetCore.Identity.UserManager`1[testLogin.Areas.Identity.Data.testLoginUser]' has been registered InvalidOperationException: Cannot resolve scoped service 'Microsoft.AspNetCore.Identity.UserManager .NET Core 2.0 Cannot resolve scoped service 'Microsoft.AspNetCore.Identity.UserManager`1[IdentityServerSample.Models.ApplicationUser]' from root provider
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM