【发布时间】:2019-07-13 12:11:16
【问题描述】:
环境:
- IdentityServer4 支持隐式流的实例
- Angular 7 客户端应用程序使用 oidc-client-js
- ASP.NET Framework Web API 资源使用IdentityServer3 Katana Access Token Validation Middleware
基本令牌发行和验证在我们的环境中运行良好。我现在正在尝试启用静默刷新技术(如documented here)。启用automaticSilentRenew 和简短的AccessTokenLifetime 后,我可以看到无提示请求在我的浏览器控制台中触发,正如我所期望的那样。
我可以看到对 IS4 的 UserInfo 端点的两个后续调用(见下面的屏幕截图)。第一个是 CORS 预检 OPTIONS 请求。在IProfileService.IsActiveAsync() 的自定义实现中的断点处,我可以看到此请求成功通过了身份验证(通过检查httpContext)。
public class ProfileService : IProfileService
{
private readonly HttpContext _httpContext;
public ProfileService(IHttpContextAccessor httpContextAccessor)
{
_httpContext = httpContextAccessor.HttpContext;
}
...
public async Task IsActiveAsync(IsActiveContext context)
{
var temp = _httpContext.User; // breakpoint here
// call external API with _httpContext.User info to get isActive
}
}
但是,对 UserInfo 端点的第二个请求 (GET) 未进行身份验证。我在IProfileService.IsActiveAsync() 中的断点显示没有经过身份验证的用户,因此我用于验证用户是否处于活动状态(调用另一个 API)的例程返回 false,它被转换为 401。我可以在失败的GET 请求中看到此标头@ 987654344@.
我已尝试指定小于每个 this 的 AccessTokenLifetime 的 IdentityTokenLifetime,但没有成功。
这是两个请求的日志:
Microsoft.AspNetCore.Hosting.Internal.WebHost:Information: Request starting HTTP/1.1 OPTIONS http://localhost:5000/connect/userinfo
Microsoft.AspNetCore.Cors.Infrastructure.CorsService:Information: CORS policy execution successful.
Microsoft.AspNetCore.Hosting.Internal.WebHost:Information: Request finished in 8.5635ms 204
Microsoft.AspNetCore.Hosting.Internal.WebHost:Information: Request starting HTTP/1.1 GET http://localhost:5000/connect/userinfo
Microsoft.AspNetCore.Cors.Infrastructure.CorsService:Information: CORS policy execution successful.
Microsoft.AspNetCore.Cors.Infrastructure.CorsService:Information: CORS policy execution successful.
IdentityServer4.Hosting.IdentityServerMiddleware:Information: Invoking IdentityServer endpoint: IdentityServer4.Endpoints.UserInfoEndpoint for /connect/userinfo
IdentityServer4.Validation.TokenValidator:Error: User marked as not active: f84db3aa-57b8-48e4-9b59-6deee3d288ad
Microsoft.AspNetCore.Hosting.Internal.WebHost:Information: Request finished in 94.7189ms 401
问题:
如何在静默刷新期间向 UserInfo 端点发送 GET 请求以验证到 HttpContext?
更新:
添加两个请求的所有标头和生成的浏览器 cookie 的屏幕截图,以调查 @Anders 的答案。
【问题讨论】:
-
您使用什么库来发出请求?您确定正在发送任何与 cookie 相关的选项吗?选项请求通常由浏览器发出,没有来自 API 调用的任何帮助(或影响),因此即使选项请求中有标头并不意味着后续请求中会有标头
-
我们使用oidc-client-js 发送请求。对于你的第二个问题,我真的不确定,但会调查。谢谢。
标签: c# identityserver4 identityserver3 oidc-client-js