相关疑难解决方法(0)

IdentityServer4 如何设置服务器 cookie 过期时间

到目前为止,我已经看到了如何为客户端 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 …

cookies identityserver4

6
推荐指数
1
解决办法
7677
查看次数

IdentityServer4强制用户重新输入凭据

我正在使用IdentityServer4和MVC客户端。当客户端会话期满时,我希望我的用户被迫再次登录。但是,无论我做什么,IdentityServer似乎都会在会话结束时自动将用户重新登录。

我在客户端的启动时间是(会话需要30秒才能测试)

services.AddAuthentication(options =>
        {
            options.DefaultScheme = "Cookies";
            options.DefaultChallengeScheme = "oidc";                                                
        })
            .AddCookie("Cookies", options => { options.ExpireTimeSpan = new TimeSpan(0, 0, 30); })
            .AddOpenIdConnect("oidc", options =>
            {
                options.SignInScheme = "Cookies";

                options.Authority = identityUrl;
                options.RequireHttpsMetadata = false;

                options.SaveTokens = true;                  
                options.GetClaimsFromUserInfoEndpoint = true;

                options.Scope.Add("Billing");
                options.Scope.Add("offline_access");

                options.UseTokenLifetime = false;                   
            });
Run Code Online (Sandbox Code Playgroud)

然后我在IdentityServer中的配置如下:

new Client
            {
                ClientId = "Test",
                ClientName = "Test",
                AllowedGrantTypes = GrantTypes.HybridAndClientCredentials,

                AlwaysIncludeUserClaimsInIdToken = true,

                RequireConsent = false,

                IdentityTokenLifetime = 30,
                AccessTokenLifetime = 30,

                ClientSecrets =
                {
                    new Secret("secret".Sha256())
                },              

                RedirectUris …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-core-mvc identityserver4

3
推荐指数
1
解决办法
2520
查看次数

标签 统计

identityserver4 ×2

asp.net-core-mvc ×1

c# ×1

cookies ×1