gpt4 book ai didi

authentication - JWT token 认证,过期 token 仍然有效,.net 核心 Web Api

转载 作者:行者123 更新时间:2023-12-04 02:40:29 26 4
gpt4 key购买 nike

我正在构建一个 .net 核心 web api。

前言 - 我已经按照 https://stormpath.com/blog/token-authentication-asp-net-core 实现了 token 身份验证和 https://dev.to/samueleresca/developing-token-authentication-using-aspnet-core .我还在 github 和 SO 上阅读了一些问题。

这也派上了用场https://goblincoding.com/2016/07/24/asp-net-core-policy-based-authorisation-using-json-web-tokens/ .

实现完这一切后,我觉得我错过了一些东西。

我创建了一个简单的 Angular 应用程序,它位于 Web 客户端中。当我进行身份验证时,客户端会收到一个 token 。我现在将它存储在 session 中(仍在开发中,因此将解决以后存储位置的安全问题)。

不确定这个( JWT (JSON Web Token) automatic prolongation of expiration )是否有用,因为就我所见,我还没有实现刷新 token 。

我注意到,当我调用 logout,然后重新登录时,客户端会收到一个新 token - 正如预期的那样。但是,如果 token 到期时间已过(我将其设置为 1 分钟进行测试)然后刷新页面,则 token 在我的应用程序中似乎保持不变。 即好像 token 永不过期?!

我本来希望客户端返回 401 Unauthorized 错误,然后我可以处理强制用户重新进行身份验证。

这不是应该如何工作吗? 后台是否有一些默认的自动刷新 token 魔法(我没有在教程中明确设置任何刷新 token 的概念)?或者我是否缺少有关 token 身份验证概念的信息?

还有 - 如果这是一个永久刷新的 token ,如果 token 曾经被泄露,我是否应该担心安全性?

谢谢你的帮助

最佳答案

我相信这与 JwtBearerOptions 中的 ClockSkew 有关。

更改为 TimeSpan.Zero 因为我相信默认设置为 5 分钟(虽然不是 100% 确定)。

我在下面发布了一些示例代码,这些代码将放置在 Startup.cs => 配置中。

        app.UseJwtBearerAuthentication(new JwtBearerOptions()
{
AuthenticationScheme = "Jwt",
AutomaticAuthenticate = true,
AutomaticChallenge = true,
TokenValidationParameters = new TokenValidationParameters()
{
ValidAudience = Configuration["Tokens:Audience"],
ValidIssuer = Configuration["Tokens:Issuer"],
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["Tokens:Key"])),
ValidateLifetime = true,
ClockSkew = TimeSpan.Zero
}
});

关于authentication - JWT token 认证,过期 token 仍然有效,.net 核心 Web Api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43045035/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com