IdentityServer4 如何强制用户重新输入凭据

5

我正在使用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;                   
            });

那么我在IdentityServer中的配置如下:

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

                AlwaysIncludeUserClaimsInIdToken = true,

                RequireConsent = false,

                IdentityTokenLifetime = 30,
                AccessTokenLifetime = 30,

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

                RedirectUris = { billingUrl + "/signin-oidc" },
                PostLogoutRedirectUris = { billingUrl + "/signout-callback-oidc" },

                AllowedScopes =
                {
                    IdentityServerConstants.StandardScopes.OpenId,
                    IdentityServerConstants.StandardScopes.Profile,
                    "Billing",
                    "role",
                    "VisionBlue.Cloud.BillingAPI"
                },

                AllowOfflineAccess = true
            },

使用 Fiddler 我可以看到在 30 秒后会向 IdentityServer 的 /connect/authorize 发送一个请求,这会自动让用户再次登录。
有什么想法吗?我已经将所有超时时间设为 30 秒进行测试。
4个回答

16

在您的授权请求中使用OpenID Connect参数prompt=login (https://openid.net/specs/openid-connect-core-1_0.html#AuthRequest)。这将告诉IdentityServer,您希望用户重新进行身份验证,而不是使用SSO或IdentityServer会话长度。

您应该能够在ASP.NET Core中使用OpenIdConnectOptions来实现此操作:

options.Events.OnRedirectToIdentityProvider = context =>
{
    context.ProtocolMessage.Prompt = "login";
    return Task.CompletedTask;
};

不过可能有更简单的方法来设置这个。


1
这是最好的答案,因为重新登录是必要的,例如当访问系统的敏感部分时,您希望确保用户确实是他自己。 - Kat Lim Ruiz
谢谢!在我经过数小时的搜索后,您拯救了我的生命。在我的情况下,我需要让用户在访问他未被授权的系统部分时注销并重定向到SSO。 - esmehsnj

2
接受的答案是正确的,但还有另一种选择,那就是max_age参数。通过它,您可以指定自auth_time以来允许的最长时间,并且如果超过此时间,它将自动提示用户重新进行身份验证。 在签署id_token时,您还可以检查auth_time声明,以确保它在您期望的时间范围内。我建议这样做,因为终端用户很容易从授权请求中删除prompt = login或max_age = xxx。

0

这只会使客户端应用程序上的cookie过期。IDS cookie也必须设置为过期。在本地和外部登录的POST方法中查找。

您需要找到创建AuthenticationProperties的部分(在Quickstart中,它是设置AuthenticationToken的位置),并在将属性传递给HttpContext.SignInAsync之前设置IsPersistentExpiresUtc

(顺便说一句,这也可以用于支持会话之间的持久登录--在IDS4中设置长时间,然后向客户端添加非默认OIDC配置文件,并在新匿名会话开始时触发一次无提示的登录尝试。)


-1

很遗憾,我仍然会自动登录,而且值被设置为false。 - keitn

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接