简体   繁体   中英

How to inject UserManager to another service in ASP.NET Core 2.0

I created a service that uses user manager identity

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

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

And I added this line to my startup.cs

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

But I had this error :

InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNet.Identity.UserManager'

Does anyone know how we can inject user manager to another service ?

I assume you are seeing this error because you have not registered all of the user managers dependencies. https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.identity.usermanager-1?view=aspnetcore-2.1 look at the constructor to see what needs to be registered to construct a UserManager. Alternative you can register all of the required identity services with an easy extension. services.AddIdentity<IdentityUser, IdentityRole>()

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM