Authentication 如何使用Identity Server更改dotnet core SPA(Angular)的JWT超时
我正在使用DotnetCore3,并试图设置JWT过期时间,但找不到在何处执行此操作。我知道这可以在创建JWT令牌的地方完成,但这是在微软的一个库中发生的。有人知道如何做到这一点吗?或者,最好有一个自动刷新令牌的系统 以下是我的Startup.cs文件的相关摘录:Authentication 如何使用Identity Server更改dotnet core SPA(Angular)的JWT超时,authentication,jwt,asp.net-identity,asp.net-core-3.0,.net-core-3.0,Authentication,Jwt,Asp.net Identity,Asp.net Core 3.0,.net Core 3.0,我正在使用DotnetCore3,并试图设置JWT过期时间,但找不到在何处执行此操作。我知道这可以在创建JWT令牌的地方完成,但这是在微软的一个库中发生的。有人知道如何做到这一点吗?或者,最好有一个自动刷新令牌的系统 以下是我的Startup.cs文件的相关摘录: public void ConfigureServices(IServiceCollection services) { services.AddDbContext<ApplicationDbContext>(opt
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
}
非常感谢您的帮助。谢谢 您可以重新配置Microsoft在AddApiAuthorization方法中创建的默认客户端
services.AddIdentityServer()
.AddApiAuthorization<ApplicationUser, ApplicationDbContext>(options =>
{
options.Clients.First().AccessTokenLifetime = 600;
});
services.AddIdentityServer()
.addapi授权(选项=>
{
options.Clients.First().AccessTokenLifetime=600;
});
创建令牌的代码在哪里?我希望应该有一个端点,从那里您将发布令牌。请求管道中的中间件将验证传入的jwt令牌,但不会创建它。还有,你在哪里为你的代币添加声明?好吧,就是这样。在我的项目中没有实现这一点的代码。它只是Identity Server黑盒的一部分。我想我只需要扔掉这个黑匣子。至少你应该传递一些要包含在令牌中的声明,通常是在我们设置令牌生存时间的方法中。你知道这一点吗?我尝试了下面提出的解决方案,但没有成功。和你一样,我使用core 3.1和angular 8作为前端,所以我也在那个黑匣子上玩得很开心:-)抱歉@eddyizm,但我没有。我只是决定不使用JWTs。我也有其他的困难,但最终还是不值得。在我看来,配置dotnetcore和Kestrel更像是巫毒,而不是软件开发。
services.AddIdentityServer()
.AddApiAuthorization<ApplicationUser, ApplicationDbContext>(options =>
{
options.Clients.First().AccessTokenLifetime = 600;
});