c# - ASP.Net identity slidingexpiration 设置为true 不重发cookie

标签 c# cookies asp.net-identity expired-cookies

我正在使用 ASP.Net 身份验证来控制我的应用程序授权,我需要在指定的非事件分钟数后终止用户 session ,我尝试通过执行以下方法来实现这一点

public void ConfigureAuth(IAppBuilder app) {
    app.CreatePerOwinContext<UserStore>(() => new UserStore());
    app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);

    app.UseCookieAuthentication(new CookieAuthenticationOptions {
        AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
        LoginPath = new PathString("/login"),
        LogoutPath = new PathString("/logout"),
        CookieDomain = ConfigurationManager.AppSettings["CookieDomain"],
        Provider = new CookieAuthenticationProvider {
            // Enables the application to validate the security stamp when the user logs in.
            // This is a security feature which is used when you change a password or add an external login to your account.  
            OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
                validateInterval: TimeSpan.FromMinutes(2),
                regenerateIdentity: (manager, user) => manager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie)
            )
        },
        SlidingExpiration = true,
    });
}

我也试过这个方法

app.UseCookieAuthentication(new CookieAuthenticationOptions {
    AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
    LoginPath = new PathString("/login"),
    LogoutPath = new PathString("/logout"),
    CookieDomain = ConfigurationManager.AppSettings["CookieDomain"],
    ExpireTimeSpan = TimeSpan.FromMinutes(2),
    SlidingExpiration = true,
});

使用这些 aproches 用户 cookie session 在 2 分钟后过期,无论用户是否在站点中处于事件状态。我在文档中读到,通过设置 SlidingExpiration = true,cookie 将根据 ExpireTimeSpan 中途的任何请求重新发布。例如,如果用户登录并在 16 分钟后发出第二个请求,则 cookie 将重新发布 30 分钟。如果用户登录并在 31 分钟后发出第二个请求,则会提示用户登录。

我不知道为什么它不起作用,有什么想法吗?

最佳答案

需要明确的是:CookieHandler 在请求刷新之前检查 cookie 过期之前的剩余时间是否小于自发布以来耗时(意味着它 > 一半过期)。这是在 Microsoft.AspNetCore.Authentication.Cookies 的 dll 中。

就我个人而言,我更希望有一个选项可以更改流逝的百分比。当您为更安全的应用程序使用非常小的超时(15 分钟或更短)时,用户在仅 7 分钟不活动后超时,因为 cookie 在它们处于事件状态的前 6 分钟内从未刷新,这非常烦人。
也许添加一个选项来使用剩余时间跨度检查来代替常量。例如,当 cookie 的剩余时间少于 {TimeSpan} 时发出刷新请求。

private void CheckForRefresh(AuthenticationTicket ticket)
{
  DateTimeOffset utcNow = this.get_Clock().get_UtcNow();
  DateTimeOffset? issuedUtc = ticket.get_Properties().get_IssuedUtc();
  DateTimeOffset? expiresUtc = ticket.get_Properties().get_ExpiresUtc();
  bool? allowRefresh = ticket.get_Properties().get_AllowRefresh();
  bool flag = !allowRefresh.HasValue || allowRefresh.GetValueOrDefault();
  if (((!issuedUtc.HasValue || !expiresUtc.HasValue ? 0 : (this.get_Options().SlidingExpiration ? 1 : 0)) & (flag ? 1 : 0)) == 0)
    return;
  TimeSpan timeSpan = utcNow.Subtract(issuedUtc.Value);
  if (!(expiresUtc.Value.Subtract(utcNow) < timeSpan))
    return;
  this.RequestRefresh(ticket);
}

关于c# - ASP.Net identity slidingexpiration 设置为true 不重发cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38983173/

相关文章:

c# - 将简单的 C# 方法转换为 jquery

javascript - 创建一个只能从 JS 访问的 cookie,并且不会随每个请求一起发送/

java - Cookies...Http POST 无法正常工作,为什么?

ruby-on-rails-3 - Rails 3 - 登录另一个站点并在 session 中保留 cookie

entity-framework - F# 中的 Entity Framework 身份管理

c# - 从 GridView 获取值(value)

c# - OpenCV + 安卓 + Unity

c# - 注册多个 DbContext 时,如何解析 ConfigureServices 中正确的 DbContext 类型

c# - 无法跨 AppDomains 传递 GCHandle : solution without delegates?

c# - SignInManager IsSignedIn 在 SignalR 集线器中返回 false