【问题标题】:How to solve "Unable to resolve service for type 'Microsoft.AspNetCore.Identity.UserManager"?如何解决“无法解析类型'Microsoft.AspNetCore.Identity.UserManager的服务”?
【发布时间】: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


【解决方案1】:

这并没有像“解决问题”中那样回答问题。但它确实解决了设置用户管理的问题,而且它似乎是@hdoitc 可以接受的答案:

首先,客户端不应该有权访问身份模型。这是 IdentityServer 的职责。登录同样重要。用户没有登录客户端网站,而是登录 IdentityServer 网站。至于注册,用户是在 IdentityServer 中注册的,而不是在客户端中。

IdentityServer 对用户进行身份验证并授权客户端。因此,您需要将用户发送到 IdentityServer 网站进行登录和注册。将用户视为未绑定到一个客户端的实体。使用它的帐户,它可以使用任何客户端,除非授权被拒绝,尽管 IdentityServer 是not recommended 用于用户授权:

PolicyServer 是我们推荐的用户授权。

如果您有兴趣,请here's 一个链接。

默认情况下,Identity 没有太多 UI,因为 IdentityServer 实际上不是关于用户管理的。但是有一些support for asp.net Identity

您可以随意添加视图,因为这是 asp.net Identity。我建议您创建一个新项目和scaffold the Identity 文件,如此处所述,并使用您需要的任何内容,或查看sources here。如果我没记错的话,RazorPages 有 UI,但 Mvc 没有。

您可以通过自定义视图来改善 IdentityServer 中的用户体验,例如添加徽标,通过 clientId 识别客户端来使用主题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-01-30
    • 2019-05-20
    • 1970-01-01
    • 2021-08-07
    • 2022-01-04
    • 1970-01-01
    • 1970-01-01
    • 2018-10-09
    相关资源
    最近更新 更多