How to inject UserManager & SignInManager

asp.net-identity asp.net-mvc-5 c# entity-framework-6 ninject

Question

Understanding how to inject UserManager and SignInManager is my current goal. I have Ninject installed in my application and I'm using it as follows:

Please treat this as a brand-new endeavor. Startup.cs contains the following:

public partial class Startup
{
    public void Configuration(IAppBuilder app)
    {
        ConfigureAuth(app);

        app.UseNinjectMiddleware(CreateKernel);
    }

    private static IKernel CreateKernel()
    {
        var kernel = new StandardKernel();
        kernel.Load(Assembly.GetExecutingAssembly());


        return kernel;
    }
}

Currently, if I were to make a dummy class and attempt to inject it based on its interface, it would work. I've tried it out. I'm attempting to find out how to extract and inject the following from Startup.Auth.cs. I'm not sure how this is done because there are no interfaces I can rely on:

app.CreatePerOwinContext(ApplicationDbContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);

Just to be clear, I want to know how to create the ApplicationUserManager and ApplicationSignInManager instances and inject them into my controller parameters. The controller into which I'm attempting to inject this is as follows:

public AccountController(ApplicationUserManager userManager, ApplicationSignInManager signInManager)
{
    UserManager = userManager;
    SignInManager = signInManager;
}

EDIT:

What I did was as follows:

private static IKernel CreateKernel()
{
    var kernel = new StandardKernel();
    kernel.Load(Assembly.GetExecutingAssembly());

    kernel.Bind<IUserStore<ApplicationUser>>().To<UserStore<ApplicationUser>>();
    kernel.Bind<UserManager<ApplicationUser>>().ToSelf();

    return kernel;
}

However, this results in a null reference error.

1
18
3/29/2016 9:18:15 PM

Accepted Answer

ZZZ_tmp
19
11/16/2018 12:16:15 PM

Popular Answer

ZZZ_tmp


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow