【问题标题】:How to change JWT timeout for dotnet core SPA (Angular) with Identity Server如何使用 Identity Server 更改 dotnet core SPA (Angular) 的 JWT 超时
【发布时间】: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


【解决方案1】:

您可以重新配置微软在 AddApiAuthorization 方法中创建的默认客户端

services.AddIdentityServer()
.AddApiAuthorization<ApplicationUser, ApplicationDbContext>(options =>
{
    options.Clients.First().AccessTokenLifetime = 600;
});

【讨论】:

    猜你喜欢
    • 2020-07-10
    • 1970-01-01
    • 2020-11-05
    • 2020-06-20
    • 2021-10-07
    • 2018-07-01
    • 2017-07-14
    • 2022-01-23
    • 1970-01-01
    相关资源
    最近更新 更多