简体   繁体   中英

ASP.NET Core Dependecy Injection in Extension Methods

I would like to extend UserManager to allow finding user by UserName. I have created a extension method. I have noticed the easiest way to achieve it is to get UserStore . I cannot do this by UserManager because Store is protected. I decided to use DI to inject UserStore into my method but I am not even sure it is possible.

I have tried to pass as a second argument IUserStore<T> store but VS says no argument has been given.

using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;

namespace Learning.Extensions
{
    public static class UserManagerExtension
    {
        public static Task<T> FindByUserNameAsync<T>(this UserManager<T> userManager, IUserStore<T> store, string userName) where T : class
        {
            var result = store.FindByNameAsync(userName, CancellationToken.None);

            return result;
        }
    }
}

You can use the existing method to search for a user by name:

public Task<TUser> UserManager<TUser>.FindByNameAsync(String);

More information can be found in ASP.NET Core Docs .

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