简体   繁体   English

扩展方法中的ASP.NET核心依赖注入

[英]ASP.NET Core Dependecy Injection in Extension Methods

I would like to extend UserManager to allow finding user by UserName. 我想扩展UserManager以允许通过UserName查找用户。 I have created a extension method. 我创建了一个扩展方法。 I have noticed the easiest way to achieve it is to get UserStore . 我注意到最简单的方法是获取UserStore I cannot do this by UserManager because Store is protected. 我无法通过UserManager进行此操作,因为Store受到了保护。 I decided to use DI to inject UserStore into my method but I am not even sure it is possible. 我决定使用DI将UserStore注入我的方法中,但我什至不确定是否有可能。

I have tried to pass as a second argument IUserStore<T> store but VS says no argument has been given. 我试图将IUserStore<T> store作为第二个参数传递,但是VS表示没有给出任何参数。

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 . 更多信息可以在ASP.NET Core Docs中找到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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