0

I am trying to log users out if their security stamp changes, but SecurityStampValidatornever checks the security stamp.
By using the code from SecurityStampValidator.cs I have found that GetUserManager from this call var manager = context.OwinContext.GetUserManager<TManager>(); always returns null.

I am using Unity for Dependency Injection. See:

public void ConfigureAuth(IAppBuilder app)
{
    CookieOptions = new CookieAuthenticationOptions
    {
        AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
        ExpireTimeSpan = TimeSpan.FromHours(24),
        Provider = new CookieAuthenticationProvider
        {
            OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
                validateInterval: TimeSpan.FromSeconds(20),
                regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager, DefaultAuthenticationTypes.ApplicationCookie))
        }
    };
    app.UseCookieAuthentication(CookieOptions);
}

.

unity.RegisterType<ApplicationSignInManager>(new HierarchicalLifetimeManager());
unity.RegisterType<ApplicationUserManager>(new HierarchicalLifetimeManager());
unity.RegisterType<IUserStore<ApplicationUser>, UserStore<ApplicationUser>>(new HierarchicalLifetimeManager());

Question: How do I inject the user manager correctly or wire up the OWIN context correctly?

Jeppe
  • 1,424
  • 2
  • 15
  • 36
  • 1
    See [this answer](https://stackoverflow.com/a/32299022/) and [this blog post](http://tech.trailmax.info/2014/09/aspnet-identity-and-ioc-container-registration/). – NightOwl888 Oct 10 '17 at 00:37
  • NightOwl888 Thanks a lot, your comment brought me a little closer. I've added `app.CreatePerOwinContext(() => DependencyResolver.Current.GetService())` to my `ConfigureAuth` and it almost works. It works with the first call to `ConfigureAuth` but on subsequent calls I get a _System.ObjectDisposedException: Cannot access a disposed object. Object name: 'ApplicationUserManager'_ error. Any idea how to fix this lifetime issue? – Jeppe Oct 10 '17 at 07:10

0 Answers0