【发布时间】:2020-07-01 08:06:34
【问题描述】:
我正在使用 dotnet core 3,并尝试设置 JWT 过期时间,但找不到在哪里设置。我知道它可以在创建 JWT 令牌的地方完成,但这发生在 Microsoft 的一个库中。有谁知道如何做到这一点?或者,最好有一个系统来自动刷新令牌。
以下是我的 Startup.cs 文件的相关摘录:
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options =>
options.UseNpgsql(Configuration.GetConnectionString("DefaultConnection")));
services.AddDefaultIdentity<ApplicationUser>(options =>
options.SignIn.RequireConfirmedAccount = true)
.AddEntityFrameworkStores<ApplicationDbContext>();
services.AddIdentityServer()
.AddApiAuthorization<ApplicationUser, ApplicationDbContext>();
services.AddAuthentication()
.AddIdentityServerJwt();
services.Configure<JwtBearerOptions>(IdentityServerJwtConstants.IdentityServerJwtBearerScheme, options =>
{
//Wouldn't this be a good place for an expiration property? Microsoft doesn't think so.
});
//Unrelated service configuration code here
}
然后在配置中:
public void Configure(...){
//Irrelevant stuff here
app.UseAuthentication();
app.UseIdentityServer();
app.UseAuthorization();
//More irrelevant stuff here
}
非常感谢任何帮助。谢谢!
【问题讨论】:
-
创建令牌的代码在哪里?我希望应该有一个端点,您将从那里发出令牌。请求管道中的中间件将验证传入的 jwt 令牌,但不会创建它。另外,您在哪里为您的令牌添加声明?
-
嗯,就是这样。我的项目中没有代码可以做到这一点。它只是作为 Identity Server 黑盒的一部分。我想我只需要扔掉黑匣子。
-
至少你应该传递一些要包含在令牌中的声明,通常在我们设置令牌生命周期的方法中
-
你有没有想过这个问题?我尝试了下面提出的解决方案无济于事。和你一样,我在前端使用 core 3.1 和 angular 8,所以我在那个黑盒子上也玩得很开心:-)
-
对不起@eddyizm,但我没有。我只是决定不使用 JWT。我也遇到了其他困难,最终这不值得。在我看来,配置 dotnet core 和 Kestrel 感觉更像是巫术而不是软件开发。
标签: authentication jwt asp.net-identity asp.net-core-3.0 .net-core-3.0