【发布时间】:2019-11-29 09:40:49
【问题描述】:
MVC 客户端:ASP.NET CORE 3 身份(带有脚手架身份)。 身份服务器 4 (v3)。
由quickstart创建。
MVC 客户端启动有:
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
AddIdentity 帮助我注册新用户。
当我尝试使用 Identity Server 登录 MVC 客户端时,出现无限循环。我read,需要删除 AddIdentity,因为我的客户端连接到 Id Server。 当我删除 AddIdentity 时,登录效果很好。
但是我的 MVC 客户端已经为用户注册搭建了身份。当我尝试打开用户注册表单 (http://localhost:5002/Identity/Account/Register) 时,出现错误:
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'.
当我尝试在没有 Identity UserManager 的情况下注册时:
services.AddScoped<UserManager<ApplicationUser>>();
services.AddScoped<SignInManager<ApplicationUser>>();
我收到其他错误:
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
也许我走错路了?我需要,我的 MVC 客户端可以注册用户,并且可以使用 Identity Server 登录用户。
【问题讨论】:
-
@Erskan 存在如何使用 AddIdentity 注册自定义 UserManager 的问题。但我需要在没有 AddIdentity 的情况下注册 UserManager。因为使用 AddIdentity 我还有其他问题 - 无限循环登录过程。我必须删除 AddIdentity。
-
你试过用 AddTransient 代替 AddScoped 吗?
-
请注意,客户端不应访问身份模型。这是 IdentityServer 的职责。登录同样重要。用户没有登录客户端网站,而是登录 IdentityServer 网站。至于注册,用户注册在 IdentityServer 而不是客户端。用户不绑定到一个客户端,但可以使用此帐户使用每个客户端。 IdentityServer 对用户进行身份验证并授权客户端。因此,您需要将用户发送到 IdentityServer 网站进行登录和注册。使用 clientId 您可以自定义 IdentityServer 视图。
-
IdentityServer 其实不是关于用户管理的,但是对asp.net Identity 有一些支持。您可以随意添加视图,因为这是 asp.net Identity。我建议您创建一个新项目并将身份文件作为documented here 搭建。或查看来源here。
标签: c# asp.net-core identityserver4