简体   繁体   English

为身份UserStore注入自定义实现

[英]Inject Custom Implementation for Identity UserStore

I have added custom implementation of UserStore. 我添加了UserStore的自定义实现。 For the user store constructor I inject UserRepository how can I register that with simple injector container 对于用户存储构造函数,我注入了UserRepository,如何使用简单的注入器容器进行注册

my code like 我的代码像

public class UserStore : IUserStore<User>, IUserLoginStore<User>, IUserPasswordStore<User>,
    IUserSecurityStampStore<User>, IUserEmailStore<User>
{
    private readonly IRepository<User> _userRepository;

    public UserStore(IRepository<User> userRepository)
    {
        _userRepository = userRepository;
    }

    //other implementations
}

my UserManager Class 我的UserManager类

public  class UserStoreManager<T> : UserManager<User> where T : class 
{ 
    public UserStoreManager(IUserStore<User> store) : base(store)
    {

    }
}

Update 更新资料

i have tried this by registering like this 我已经尝试过这样注册

container.Register<IUserStore<User>, UserStore>(Lifestyle.Scoped);
container.Register<UserManager<User>, UserStoreManager<User>>(Lifestyle.Scoped);

then i got an exception 然后我有一个例外

在此处输入图片说明

When I need to initialize UserStore I just use the following 当我需要初始化UserStore ,只需使用以下内容

container.RegisterPerWebRequest<IUserStore<User>>(() => new UserStore<User>((IRepository<User>)container.GetInstance<IRepository<User>>()));

UserStore needs the an instance of the context, however it is only possible to get the current context with GetInstance since it is "calculated on runtime", depending on the Context lifestyle UserStore需要上下文的一个实例,但是只能通过GetInstance获取当前上下文,因为它是“在运行时计算的”,具体取决于上下文的生活方式

Edit 1 编辑1

For UserManager you need to do the following: 对于UserManager您需要执行以下操作:

container.RegisterPerWebRequest(() => new ApplicationUserManager(container.GetInstance<IUserStore<ApplicationUser>>(), DataProtectionProvider));

It follows the same principle as the above. 它遵循与上述相同的原理。

DataProtectionProvider is an argument of my method called InitializeContainer DataProtectionProvider是我的名为InitializeContainer方法的参数

private static void InitializeContainer(Container container, IDataProtectionProvider DataProtectionProvider)
{
    /* OMITTED */
    container.RegisterPerWebRequest(() => new ApplicationUserManager(container.GetInstance<IUserStore<ApplicationUser>>(), DataProtectionProvider));
}

which is used on: 用于:

public static void InitializeInjector(this IAppBuilder app, IDataProtectionProvider DataProtectionProvider)
{
    var container = new Container();
    container.Options.DefaultScopedLifestyle = new WebRequestLifestyle();
    container.Options.PropertySelectionBehavior = new PropertySelectionBehavior<InjectAttribute>();

    InitializeContainer(container, DataProtectionProvider); // Here
    app.UseOwinContextInjector(container);
    app.MapSignalR(container);

    container.RegisterMvcControllers(Assembly.GetExecutingAssembly());
    container.Verify();
    DependencyResolver.SetResolver(new SimpleInjectorDependencyResolver(container));

    BinderConfig.RegisterModelBinders(container);
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters, container);
}

which is called on 被称为

public void Configuration(IAppBuilder app)
{
    ConfigureAuth(app);
    app.InitializeInjector(app.GetDataProtectionProvider());
}

So, DataProtectionProvider comes from the IAppBuilder 因此, DataProtectionProvider来自IAppBuilder

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

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