C# MVC 5中具有ASP.NET标识的Autofac无法验证OWIN管道中的安全戳

C# MVC 5中具有ASP.NET标识的Autofac无法验证OWIN管道中的安全戳,c#,asp.net-mvc,asp.net-identity,owin,autofac,C#,Asp.net Mvc,Asp.net Identity,Owin,Autofac,我将AutoFac设置为在MVC5中使用ASP.NET标识。表面上看来一切都很正常,即用户可以创建帐户并登录。但后来我发现,当安全戳被更改时,用户不会注销。通过AspNetUsers表中的暴力,或者通过用户更改密码并希望在其他浏览器中注销 下面是我如何设置AutoFac的 public void配置(IAppBuilder应用程序) { var builder=new ContainerBuilder(); builder.RegisterType().AsSelf().InstancePerR

我将AutoFac设置为在MVC5中使用ASP.NET标识。表面上看来一切都很正常,即用户可以创建帐户并登录。但后来我发现,当安全戳被更改时,用户不会注销。通过AspNetUsers表中的暴力,或者通过用户更改密码并希望在其他浏览器中注销

下面是我如何设置AutoFac的

public void配置(IAppBuilder应用程序)
{
var builder=new ContainerBuilder();
builder.RegisterType().AsSelf().InstancePerRequest();
builder.RegisterType().As().InstancePerRequest();
builder.RegisterType().AsSelf().InstancePerRequest();
builder.RegisterType().AsSelf().InstancePerRequest();
Register(c=>HttpContext.Current.GetOwinContext().Authentication.InstancePerRequest();
注册(c=>app.GetDataProtectionProvider()).InstancePerRequest();
注册控制器(类型化(MVCAPApplication).Assembly);
var container=builder.Build();
SetResolver(新的AutofacDependencyResolver(容器));
app.useautofac中间件(容器);
app.UseAutofacMvc();
ConfigureAuth(app);
}
这就是我如何设置cookie身份验证中间件的方法。除了验证间隔较短的时间跨度之外,它是默认值

public void ConfigureAuth(IAppBuilder app)
{
    app.UseCookieAuthentication(new CookieAuthenticationOptions
    {
        AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
        LoginPath = new PathString("/Account/Login"),
        Provider = new CookieAuthenticationProvider
        {
            OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
                validateInterval: TimeSpan.FromSeconds(15),
                regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
        }
    });            
}
public void ConfigureAuth(IAppBuilder应用程序)
{
app.UseCookieAuthentication(新的CookieAuthenticationOptions
{
AuthenticationType=DefaultAuthenticationTypes.ApplicationOkie,
LoginPath=新路径字符串(“/Account/Login”),
Provider=新CookieAuthenticationProvider
{
OnValidateIdentity=SecurityStampValidator.OnValidateIdentity(
validateInterval:TimeSpan.FromSeconds(15),
regenerateIdentity:(管理器,用户)=>user.GenerateUserIdentityAsync(管理器))
}
});            
}

如果我在GenerateUserIdentityAsync中设置断点,则仅当用户第一次登录时才会调用它。

安全戳验证器需要
ApplicationUserManager
,它会尝试从OWIN上下文解析实例(因为它不知道更好的方法)。因此,您仍然需要向OWIN注册
ApplicationsRemanager

app.CreatePerOwinContext(() => DependencyResolver.Current.GetService<ApplicationUserManager>());
app.CreatePerOwinContext(()=>DependencyResolver.Current.GetService());
app.CreatePerOwinContext(() => DependencyResolver.Current.GetService<ApplicationUserManager>());