asp.net-mvc - 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
}
解决方案
声明没有缓存层。ClaimsPrincipal
每次运行受保护的 ( [Authorize]
) 端点时,都会重新构建声明以及声明。这是由身份验证中间件完成的。通常,您将拥有 cookie 身份验证方案,该方案允许您避免每次都返回 UserInfo 端点,并且通常会重新验证令牌,直到它过期或身份验证 cookie 被有效删除(通过注销或其他方式)。
推荐阅读
- amazon-web-services - 如何使用根账户覆盖组织单位内的 AWS S3 存储桶策略
- java - FreeMarker:左侧操作数:预期为哈希,但这已评估为数字(包装器:ftSimpleNumber)
- javascript - Android 应用上的 Cordova 音量按钮功能
- azure-devops - 我们可以在 VSTS 上有团队明智的燃尽图和看板吗?
- node.js - Nodejs mongoose 在一个查询中从两个集合中获取结果
- sql - 在多个子查询中使用同一个表时是否需要表别名
- python - Python时间戳数据框仅数字/十进制过滤
- java - 将 PDFBox 迁移到 2.x 时出现 GetPages 错误
- git - 怎么说 HEAD~20 如果存在,否则先提交
- python - windows 10 pip install '连接失败'