如何与身份服务器4保持联系?

时间:2018-04-14 07:52:30

标签: c# identityserver4 oidc

我在这里尝试实施"保持签约"身份服务器4的功能,如果用户没有注销它将保持登录无限时间,但我不能。我已尝试使用客户端设置来无限制地增加令牌生存时间(一个大数字),但它不起作用。你做过这个吗?

通过将以下任意组合设置为某些值,可以实现这一点吗?

AuthorizationCodeLifetime = 5, 
IdentityTokenLifetime = 5,                
AccessTokenLifetime = 5, 
AllowOfflineAccess = true,                  
AbsoluteRefreshTokenLifetime = 5, 
RefreshTokenUsage =  TokenUsage.ReUse,
RefreshTokenExpiration = TokenExpiration.Absolute,
UpdateAccessTokenClaimsOnRefresh = true, 
SlidingRefreshTokenLifetime = 5, 
AllowRememberConsent = false

有没有不同的方法来实现这些功能?

这是完整的客户端配置:

            new Client
            {
                ClientId = "uilocal",
                ClientName = "UI development",
                AllowedGrantTypes = GrantTypes.Implicit,
                AllowAccessTokensViaBrowser = true,
                RequireConsent = false,

                RedirectUris = {"http://localhost:5004/index.html"},
                PostLogoutRedirectUris = 
               {"http://localhost:5004/index.html"},
                AllowedCorsOrigins = {"http://localhost:5004"},

                AllowedScopes =  new List<string>
                    {
                       IdentityServerConstants.StandardScopes.OpenId,
                       IdentityServerConstants.StandardScopes.OfflineAccess,
                       IdentityServerConstants.StandardScopes.Profile,
                       IdentityServerConstants.StandardScopes.Email,
                       "api1",
                       "mvc5",
                       "jsreport"
                    };

                AuthorizationCodeLifetime = 5,
                IdentityTokenLifetime = 5,
                AccessTokenLifetime = 5,

                AllowOfflineAccess = true,
                AbsoluteRefreshTokenLifetime = 5,
                RefreshTokenUsage =  TokenUsage.ReUse,
                RefreshTokenExpiration = TokenExpiration.Absolute,
                UpdateAccessTokenClaimsOnRefresh = true,
                SlidingRefreshTokenLifetime = 5,
                AllowRememberConsent = false
             }

这里提到我在登录时启用了IsPersistent:

    AuthenticationProperties properties = null;
         if (loginInputModel.RememberLogin)
          {
              properties = new AuthenticationProperties
                {
                  IsPersistent = true
                };
          }

 HttpContext.Authentication.SignInAsync(user.Id, user.UserName, properties);

这里要提到的是我在使用Implicit from angular application,通过传递带有请求标头的访问令牌来使用API​​。

对此有任何帮助,请提前表示赞赏。感谢。

1 个答案:

答案 0 :(得分:2)

似乎不清楚如何使用令牌。

在登录请求时,访问令牌和刷新令牌(包括令牌请求中的offline_access范围)。 refresh token在混合,授权代码和资源所有者密码流中可用。

访问令牌应该是短暂的(例如五分钟,一小时),而刷新令牌应该是长寿命的。例如。 30天。

为什么呢?因为访问令牌很难被撤销,以防它被截获。它是一个独立的包,不能更改(替代可以是引用令牌)。因此,出于安全考虑,它应该是短暂的。这使得“破解”的吸引力降低。

这就是为什么刷新令牌应该是长寿的,你需要它来请求一个新的访问令牌。问题在于,永远不应该截获刷新令牌,因为它会在到期之前提供无限制访问。所以客户必须能够“保守秘密”。然而,撤销刷新令牌更容易。您还可以在每次访问令牌请求时“更新”刷新令牌。

刷新令牌仅用于请求新的访问令牌。您可以随时请求新令牌,即使当前访问令牌尚未过期。但是,当访问令牌即将过期或过期时,您更有可能请求新令牌。

您可以将刷新令牌发送到IdentityServer,而无需打扰用户输入凭据。

依此类推,直到刷新令牌本身到期为止。在这种情况下,您将强制用户至少每30天登录一次。但如果你不想要,你可以选择使用滑动过期。