首页 > 解决方案 > IdentityServer4 参考令牌缓存选项

问题描述

我使用 IdentityServer4 并希望将其用于我的微服务。
我现在有两个服务:
- AuthService
- MVC 站点
我想使用生命周期较短的引用令牌来经常从 AuthService 请求实际声明,但我找不到用于设置缓存生命周期的属性。

如何为索赔配置缓存时间,为用户获取实际索赔是否是个好主意?

我尝试设置 AccessTokenLifeTime、IdentityTokenLifeTime、TokenValidationParameters.ClockSkew,但它不适用于此任务。

MVC 启动:

...
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
services.AddAuthentication(options =>
                {
                    options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                    options.DefaultChallengeScheme = "oidc";
                })
                .AddCookie("Cookies")
            .AddOpenIdConnect("oidc", options =>
                {
                    options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                    options.Authority = "https://localhost:5001";
                    options.ClientId = "client";
                    options.ClientSecret = "secret";
                    options.ResponseType = "code id_token";

                    options.RequireHttpsMetadata = false;

                    options.Scope.Add(IdentityServerConstants.StandardScopes.OpenId);
                    options.Scope.Add(IdentityServerConstants.StandardScopes.Profile);
                    options.Scope.Add("epp");
                    options.Scope.Add("roles");
                    options.TokenValidationParameters = new TokenValidationParameters
                    {
                        NameClaimType = "name",
                        RoleClaimType = "role",
                        ClockSkew = TimeSpan.FromSeconds(10)
                    };
                });
...

身份验证服务,Config.cs:

...
new Client
                {
                    ClientId = "client",
                    ClientName = "Display name",
                    AllowedGrantTypes = new List<string>{GrantType.Hybrid},
                    ClientSecrets = new List<Secret>
                    {
                        new Secret("secret".Sha256())
                    },
                    RequireConsent = false,
                    AllowedScopes =
                    {
                        IdentityServerConstants.StandardScopes.OpenId,
                        IdentityServerConstants.StandardScopes.Profile,
                        "epp",
                        "roles",
                    },
                    RedirectUris = new List<string>
                    {
                        "https://localhost:5003/signin-oidc"
                    },
                    PostLogoutRedirectUris = new List<string>{ "https://localhost:5003/signout-callback-oidc" },

                    AccessTokenType = AccessTokenType.Reference,
                    AlwaysIncludeUserClaimsInIdToken = true,
                    AlwaysSendClientClaims = true,
                    AllowAccessTokensViaBrowser = true,
                    AccessTokenLifetime = 10,
                    IdentityTokenLifetime = 10,
                    UpdateAccessTokenClaimsOnRefresh = true
                }

标签: asp.net-mvcauthenticationtokenidentityserver4

解决方案


声明没有缓存层。ClaimsPrincipal每次运行受保护的 ( [Authorize]) 端点时,都会重新构建声明以及声明。这是由身份验证中间件完成的。通常,您将拥有 cookie 身份验证方案,该方案允许您避免每次都返回 UserInfo 端点,并且通常会重新验证令牌,直到它过期或身份验证 cookie 被有效删除(通过注销或其他方式)。


推荐阅读