c# - 从 IdentityServer4 openIdConnect 获取 IsPersistent 设置

标签 c# identityserver4 openid-connect

我有一个 asp.net MVC/angular 应用程序(不是 .net core)使用 OpenIdConnect 对我们的 IdentityServer4 服务器进行身份验证。身份服务器上的登录页面有一个“记住我”复选框,用于设置身份服务器 cookie 的过期时间。如果未设置复选框,则 cookie 过期时间设置为已过期,以便在浏览器关闭时刷新 cookie。客户端 MVC 应用程序还构建了一个客户端应用程序 cookie。我希望该 cookie 的到期时间与身份服务器 cookie 的到期时间相匹配。我可以在身份服务器和客户端上将 cookie 过期值设置为相同,但这种情况仅在持久状态匹配时才有效。

前任。如果用户在身份服务器登录时没有选择记住我,并且客户端应用程序像往常一样使用过期时间,则在浏览器关闭时身份服务器 cookie 将被刷新,客户端应用程序 cookie 不会被删除。下次用户导航到该站点时,它在用户看来就好像他们已登录。一旦他们尝试访问受身份服务器保护的页面(我们使用 ResourceAuthorization),他们就会被重定向到身份服务器登录页面。我宁愿删除客户端 cookie,这样当用户返回时,它看起来不像他们仍在登录。

我正在查看 SecurityTokenValidated 事件中的 OpenIdConnectAuthenticationNotifications。这是 OpenIdConnect 成功身份验证返回的地方。在这里,我们构建了客户端应用程序 authenticationTicket。创建此票证时,最好至少知道身份服务器的 cookie 是否持久或该 cookie 的到期时间是什么。这里的 protocolMessage 似乎只包含有关 token 的信息。 token 过期与 cookie 过期不匹配。有没有办法让这些 cookie 同步?我错过了什么吗?

最佳答案

旧帖。回答以便任何其他开发人员可以使用该方法。
这就是我解决它的方法
在身份登录完成后,我们可以设置一个保存持久性值的 cookie。

var result = await _signInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, lockoutOnFailure: false);
if (result.Succeeded)
{
    _logger.LogInformation("User logged in.");
     this.SetPersistanceCookie(model.RememberMe);
     return RedirectToLocal(returnUrl);
}

private void SetPersistanceCookie(bool rememberMe)
{
        // setting persistant cookie, so that client app can set persistance of the application cookie in accordance with identity cookie
        var persistanceCookieOptions = new CookieOptions
        {
            HttpOnly = true,
            Secure = true,
        };
        HttpContext.Response.Cookies.Append("IsPersistant", rememberMe.ToString(), persistanceCookieOptions);
}
现在设置了此 cookie,您可以使用此 cookie 值在 OnTicketReceived 事件中设置应用程序 cookie 的持久性。
options.Events.OnTicketReceived = async context =>
{
// if for any reason we don't get IsPersistant Cookie (Which is used to set persistance of application cookie from identity cookie)
// we will set Application Cookie to non persistant (i.e. session life time)
       bool rememberMe = context.Request.Cookies.ContainsKey("IsPersistant")
             ? context.Request.Cookies["IsPersistant"].Contains("True")
             : false;
       var authenticationProperties = new AuthenticationProperties()
       {
            IsPersistent = rememberMe,
            ExpiresUtc = DateTime.UtcNow.AddDays(6),
            IssuedUtc = DateTime.UtcNow
       };

       context.Properties = authenticationProperties;
       await Task.FromResult(0);
};

关于c# - 从 IdentityServer4 openIdConnect 获取 IsPersistent 设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49927643/

相关文章:

oauth-2.0 - IdentityServer4 JWT 范围作为空格分隔的字符串而不是数组?

security - 使用 openid connect 作为多个应用程序的 SSO

oauth-2.0 - 根据 Keycloak 生成的 JWT 判断用户是否有权访问特定资源

c# - 脚本旋转对象超出了我的需要

c# - SqlBulkCopy 是否自动启动事务?

c# - 为 Amazon OpenSearch 设置 Serilog

asp.net - 当客户端cookie滑动时刷新IdentityServer cookie

c# - 如何在IdentityServer4/Identity中配置角色声明名称

mobile - 移动应用程序如何获得对其 API 的长期访问权限?

c# - 是否有 gtk# 应用程序的自动化框架?