【问题标题】:Obtain IsPersistent setting from IdentityServer4 openIdConnect从 IdentityServer4 openIdConnect 获取 IsPersistent 设置
【发布时间】:2018-09-30 08:16:27
【问题描述】:

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

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

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

【问题讨论】:

  • 我也在寻找类似的东西,我必须通知客户端,例如 cookie 是否持久,或者我必须包含表明它的声明。但是我如何在 IProfileService 中发出声明时访问 IsPersistent/RememberMe

标签: c# identityserver4 openid-connect


【解决方案1】:

旧帖。回答以便任何其他开发人员都可以使用该方法。

我就是这样解决的

在身份完成登录后,我们可以设置一个保存持久性值的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);
};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-10-27
    • 1970-01-01
    • 2019-04-28
    • 1970-01-01
    • 2017-09-29
    • 2018-10-19
    • 2019-02-07
    • 2018-09-25
    相关资源
    最近更新 更多