代码之家  ›  专栏  ›  技术社区  ›  Jaylen

即使在浏览器使用Identity ASP关闭后,如何保持用户登录。NET MVC框架?

  •  2
  • Jaylen  · 技术社区  · 6 年前

    SignInManager.SignIn(user, false, false);
    

    下面是我今天的身份验证配置

    public void ConfigureAuth(IAppBuilder app)
    {
        app.CreatePerOwinContext(ApplicationDbContext.Create);
        app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
        app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);
    
        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {        
            AuthenticationType = "SomeCustomName",
            LoginPath = new PathString("/Account/Login"),
            Provider = new CookieAuthenticationProvider
            {
                OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
                    validateInterval: TimeSpan.FromMinutes(60),
                    regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
            },
            SlidingExpiration = false,
            ExpireTimeSpan = TimeSpan.FromMinutes(60)
        });
    
        app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
        app.UseTwoFactorSignInCookie(DefaultAuthenticationTypes.TwoFactorCookie, TimeSpan.FromMinutes(3));
        app.UseTwoFactorRememberBrowserCookie(DefaultAuthenticationTypes.TwoFactorRememberBrowserCookie);
    }
    

    即使他/她关闭了浏览器,我如何保持登录60分钟?

    1 回复  |  直到 6 年前
        1
  •  3
  •   Igor    6 年前

    你应该做2个改变。

    AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
    SlidingExpiration = true,
    

    第一 发出cookie的时间。

    最后,登录呼叫应该会通过 true

    SignInManager.SignIn(user, true, false);
    

    旁注

    为了安全起见,设置选项也不错 CookieHttpOnly = true 这确保了脚本/客户端代码无法访问cookie。