IdentityServer4强制用户重新输入凭据

时间:2018-02-08 09:01:05

标签: c# asp.net-core-mvc 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;                   
            });

然后我在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 个答案:

答案 0 :(得分:9)

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

您应该可以在OpenIdConnectOptions中使用ASP.NET Core执行此操作:

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

虽然可能有一种更简单的方法来设置它。

答案 1 :(得分:1)

接受的答案是正确的,但还有另一个选项,那就是 max_age 参数。使用它可以指定自 auth_time 以来的最长允许时间,如果超过此时间,它将自动提示用户再次进行身份验证。 您还可以在签名时检查 id_token 中的 auth_time 声明,以确保它在您想要的窗口中。我建议这样做,因为最终用户可以根据需要从授权请求中删除prompt = login或max_age = xxx。

答案 2 :(得分:-1)

如果我在这里阅读: http://docs.identityserver.io/en/release/topics/refresh_tokens.html

此行开启自动重新登录: AllowOfflineAccess = true

将其设置为false,这可以解决您的问题。

答案 3 :(得分:-1)

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

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

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

相关问题