我在 MVC 5 中设置了 AutoFac 来与 ASP.NET Identity 一起使用。表面上一切似乎都工作正常,即用户可以创建帐户并登录。但后来我发现,当安全标记更改时,用户不会注销。通过在 AspNetUsers 表中进行暴力破解,或者通过用户更改密码并期望在其他浏览器中注销。
这就是我通过以下方式设置 AutoFac 的方式这篇非官方文章 https://developingsoftware.com/configuring-autofac-to-work-with-the-aspnet-identity-framework-in-mvc-5/.
public void Configuration(IAppBuilder app)
{
var builder = new ContainerBuilder();
builder.RegisterType<ApplicationDbContext>().AsSelf().InstancePerRequest();
builder.RegisterType<ApplicationUserStore>().As<IUserStore<ApplicationUser>>().InstancePerRequest();
builder.RegisterType<ApplicationUserManager>().AsSelf().InstancePerRequest();
builder.RegisterType<ApplicationSignInManager>().AsSelf().InstancePerRequest();
builder.Register<IAuthenticationManager>(c => HttpContext.Current.GetOwinContext().Authentication).InstancePerRequest();
builder.Register<IDataProtectionProvider>(c => app.GetDataProtectionProvider()).InstancePerRequest();
builder.RegisterControllers(typeof(MvcApplication).Assembly);
var container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
app.UseAutofacMiddleware(container);
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))
}
});
}
如果我设置断点生成用户身份异步那么只有当用户第一次登录时才会调用它。
安全印章验证器需求ApplicationUserManager
它尝试从 OWIN 上下文解析实例(因为它不知道更好)。所以你还需要注册ApplicationUsreManager
与欧文:
app.CreatePerOwinContext(() => DependencyResolver.Current.GetService<ApplicationUserManager>());
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)