【问题标题】:How to inject UserManager to another service in ASP.NET Core 2.0如何将 UserManager 注入 ASP.NET Core 2.0 中的另一个服务
【发布时间】:2018-11-20 02:43:30
【问题描述】:

我创建了一个使用用户管理员身份的服务

public class UserManagerService : IUserManagerService
{
    private readonly UserManager<IdentityUser> userManager;

    public UserManagerService(UserManager<IdentityUser> userManager)
    {
        this.userManager = userManager;
    }}

我将这一行添加到我的 startup.cs 中

services.AddTransient<IUserStore<IdentityUser>, UserStore<IdentityUser>>();
services.AddTransient<UserManager<IdentityUser>>();

但我有这个错误:

InvalidOperationException:无法解析类型“Microsoft.AspNet.Identity.UserManager”的服务

有谁知道我们如何将用户管理器注入另一个服务?

【问题讨论】:

    标签: c# asp.net-core asp.net-identity asp.net-core-2.0 usermanager


    【解决方案1】:

    我假设您看到此错误是因为您尚未注册所有用户管理器依赖项。 https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.identity.usermanager-1?view=aspnetcore-2.1看构造函数,看看构造UserManager需要注册什么。或者,您可以通过简单的扩展注册所有必需的身份服务。 services.AddIdentity&lt;IdentityUser, IdentityRole&gt;()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-22
      • 2022-01-12
      • 2013-12-22
      • 2015-06-17
      • 2012-08-01
      • 2014-01-27
      • 2019-10-22
      相关资源
      最近更新 更多