将UserManager注入CookieAuthenticationProvider失败

时间:2017-10-09 09:10:55

标签: c# asp.net dependency-injection asp.net-identity owin

如果用户的安全标记发生变化,我会尝试将用户注销,但SecurityStampValidator从不检查安全标记 通过使用SecurityStampValidator.cs中的代码,我发现此调用GetUserManager中的var manager = context.OwinContext.GetUserManager<TManager>();始终返回null。

我使用Unity进行依赖注入。参见:

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());

问题:如何正确注入用户管理器或正确连接OWIN上下文?

0 个答案:

没有答案