ASPNET Identity Caching with Dependency Injection

2019-09-04 05:52发布

问题:

I am using Ninject in an MVC application with ASPNET Identity 2.

Log in, log out, updating password etc are all working fine. However if I update the user's name in the database, the ApplicationUserManager seems to be caching the user information.

This is the code to register the Identity services:

private void RegisterServices()
{
    kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();

    var dbContext = kernel.Get<ApplicationDbContext>();
    kernel.Bind<IUserStore<ApplicationUser>>().To<UserStore<ApplicationUser>>().WithConstructorArgument("context", dbContext);

    kernel.Bind<IAuthenticationManager>().ToMethod(x => HttpContext.Current.GetOwinContext().Authentication);
}

I have posted a sample solution on GitHub. WebApplication1 works without DI, and WebApplication2 with Ninject shows the caching behaviour.

Any advice on using ASPNET identity with DI would be appreciated.