Search code examples
c#asp.net-mvcsimple-injector

Inject Custom Implementation for Identity UserStore


I have added custom implementation of UserStore. For the user store constructor I inject UserRepository how can I register that with simple injector container

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

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

enter image description here


Solution

  • When I need to initialize UserStore I just use the following

    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

    Edit 1

    For UserManager you need to do the following:

    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

    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