我当前正在构建具有身份验证/授权的WebApp来访问它,并访问多个WebAPI,所有这些都指向Identity Server 4主机。我已经遵循了IdentityServer4及其演示的官方文档,并进行了客户端身份验证,令牌生成,用户登录,使用令牌成功调用API的操作,显然,这些方法都可以正常工作,但是最近我注意到,经过一段时间的不活动之后,对API开始接收401,但客户端应用程序仍使用相同的令牌启动。
是这样的:
现在是问题(在上一步4之后)
我发现令牌已过期,Visual Studio输出指出了该令牌(还检查了https://jwt.io/上的令牌,我可以确认日期时间)。为什么相同的令牌对于ClientApp可以正常工作,而调用API却不能呢?由于API的调用,我是否需要手动生成新令牌?
我正在使用的配置是:
-客户申请---
new Client
{
ClientId = "idWebApp",
ClientSecrets = new List<Secret> { new Secret("secret".Sha256()) },
AllowedGrantTypes = GrantTypes.Hybrid,
AllowAccessTokensViaBrowser = false,
EnableLocalLogin = true,
RedirectUris = { "http://localhost:5901/signin-oidc" },
FrontChannelLogoutUri = "http://localhost:5901/signout-oidc",
PostLogoutRedirectUris = { "http://localhost:5901/signout-callback-oidc" },
AllowOfflineAccess = true,
AllowedScopes = new List<string>
{
IdentityServerConstants.StandardScopes.OpenId,
IdentityServerConstants.StandardScopes.Profile,
IdentityServerConstants.StandardScopes.OfflineAccess,
"apiAccess",
},
RequireConsent = false,
}
-API资源---
(只需使用简单的ctor来初始化'Name')
new ApiResource("apiAccess")
-自定义声明---
new IdentityResource()
{
Name = "appCustomClaims",
UserClaims = new List<string>()
{
"customRole"
}
}
-ClientClient的启动代码-]
services.AddAuthentication(options =>
{
options.DefaultScheme = "Cookies";
options.DefaultChallengeScheme = "oidc";
})
.AddCookie("Cookies")
.AddOpenIdConnect("oidc", options =>
{
options.Authority = "http://localhost:5900";
options.RequireHttpsMetadata = false;
options.ClientId = "idWebApp";
options.ClientSecret = "secret";
options.ResponseType = "code id_token";
options.Scope.Add("profile");
options.Scope.Add("offline_access");
options.ClaimActions.MapUniqueJsonKey("offline_access", "offline_access");
options.Scope.Add("appCustomClaims");
options.ClaimActions.MapJsonKey("customRole", "customRole");
options.Scope.Add("apiAccess");
options.GetClaimsFromUserInfoEndpoint = true;
options.SaveTokens = true;
options.TokenValidationParameters.RoleClaimType = "customRole";
});
我当前正在构建具有身份验证/授权的WebApp来访问它,并访问多个WebAPI,所有这些都指向Identity Server 4主机。我遵循了官方文档...
为什么相同的令牌在调用API时对ClientApp正常工作不?