Jak*_*keJ 6 cookies identityserver4
到目前为止,我已经看到了如何为客户端 webapp 的 cookie 设置过期时间(谢谢 v0id):IdentityServer4 cookie expires
IdentityServer4 实际上使用了两个 cookie——客户端 cookie 和服务器 cookie(“idsrv”)。
如果我按照此处给出的方式设置客户端 cookie 过期时间: IdentityServer4 cookie 过期时间, 那么当我关闭浏览器并返回到需要授权的客户端 webapp 页面时,我的访问被拒绝,因为浏览器会话不再具有服务器 cookie。
所以我需要一种方法来将“idsrv”cookie 过期时间设置为与客户端相同。
目前,我看到的设置服务器 cookie(它被忽略或以某种方式删除)的最佳方法是 IdentityServer4 主机 Startup.cs / ConfigureServices() 方法中的以下代码块:
services.AddIdentityServer(options =>
{
options.Authentication.CookieLifetime = new TimeSpan(365, 0, 0, 0);
options.Authentication.CookieSlidingExpiration = true;
})
Run Code Online (Sandbox Code Playgroud)
这应该将 cookie 的到期时间设置为一年后。但是,在应用程序选项卡下的 Chrome 开发人员工具 cookie 中,我看到它仍然具有 1969 年的过期过期默认日期。
我下载了 IdentityServer4 项目源,删除了 nuget 包,并将源项目添加到我的解决方案中,以便我可以通过它进行调试。
我看到它得到了我在 ConfigureInternalCookieOptions.cs / Configure() 方法中给它的到期时间。它也匹配内部的 DefaultCookieAuthenticationScheme/应用属性。我没有发现任何特定于 IdentityServer 的东西会忽略我设置的到期日期,但它仍然有 1969 年的到期日期。
编辑:我尝试将 cookie 持久设置在 IdentityServer 主机的 AccountController 中,如下所示(有趣的是,Microsoft 有一篇关于在不使用 AspNet Identity 的情况下使用身份验证属性的好文章:https ://docs.microsoft.com/en-us/ aspnet/core/security/authentication/cookie?tabs=aspnetcore2x - 它在 cookie 中发送信息,“方案”只是 cookie 名称):在 ExternalLoginCallback() 中:
if (id_token != null)
{
props = new AuthenticationProperties();
props.ExpiresUtc = DateTimeOffset.UtcNow.Add(AccountOptions.RememberMeLoginDuration);
props.IsPersistent = true;
props.StoreTokens(new[] { new AuthenticationToken { Name = "id_token", Value = id_token } });
}
Run Code Online (Sandbox Code Playgroud)
服务器端 cookie 都没有设置过期时间(AccountOptions RememberMeLoginDuration 也设置为 365 天)。“idsrv”和“idsrv.session”的有效期仍然是 1969 年。
当您在 中注册 Identity Server 时,您可以配置 Identity Server 的身份验证 cookie 生命周期Startup.cs,如下所示:
services.AddIdentityServer(options =>
{
options.Authentication.CookieLifetime = TimeSpan.FromHours(10);
})
Run Code Online (Sandbox Code Playgroud)
注意:您还需要指示用户登录时 cookie 应该是持久的。如果您使用的是快速入门 UI,则必须在登录屏幕上勾选“记住我”复选框才能获取持久 cookie。或者您可以修改代码以始终发出持久 cookie - 如下所示:
HttpContext.SignInAsync(subject, name, new AuthenticationProperties{ IsPersistent = true});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7677 次 |
| 最近记录: |