首页 > 解决方案 > 带有滑动过期的 ASP.Net 外部 Cookie 显示为会话 cookie

问题描述

我正在尝试在 Asp.Net 中配置一个滑动过期 cookie。我希望 cookie 出现在 Google Chrome 开发人员工具 cookie 管理器中,并在身份验证后 5 分钟到期,但它显示为“会话”,并且在单击退出按钮之前永不过期。如果浏览器关闭,它确实会消失。

Cookie 始终设置为会话 Cookie

以下是目前的代码。该网站使用基于 Saml 的单点登录身份验证和Kentor.AuthServicesnuget 包(现在称为SustainSys.Saml2,我们落后于版本)。

app.CreatePerOwinContext(ApplicationDbContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);

app.UseCookieAuthentication(new CookieAuthenticationOptions
{
    AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
    LoginPath = new PathString("/signin"),
    CookieSecure = CookieSecureOption.SameAsRequest,
    ExpireTimeSpan = TimeSpan.FromMinutes(5),
    SlidingExpiration = true,
    Provider = new CookieAuthenticationProvider
    {
        OnApplyRedirect = ctx => { },
        OnResponseSignIn = context =>
        {
            context.Properties.AllowRefresh = true;
            context.Properties.ExpiresUtc = DateTimeOffset.UtcNow.AddMinutes(5);
        }
    }
});

app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);

Kentor.AuthServices.Configuration.Options.GlobalEnableSha256XmlSignatures();

OnResponseSignIn最近根据此 MSDN 答案添加了 该块: https ://forums.asp.net/t/2121970.aspx?OWIN+Authentication+ExpireTimeSpan+not+working

我希望 cookie 在 30 分钟的非活动时间内过期。上面的代码设置为 5 以便于测试。

标签: asp.netcookiesowin-middlewarekentor-authservicessustainsys-saml2

解决方案


开发者工具显示 cookie 过期时间。这与身份验证令牌到期时间没有直接关系,实际上这对于您的代码也应该是正确的。

如此注释所示“过期信息存储在受保护的 cookie 票证中”。令牌过期时间应该正确生效,即使您在开发者工具中看不到它,因为它在 cookie 本身内被加密。


推荐阅读