【问题标题】:Configuring JWT Token Validations Parameter cannot be set in ASP Net Core无法在 ASP Net Core 中设置配置 JWT 令牌验证参数
【发布时间】:2021-10-29 10:23:19
【问题描述】:

我有一个场景,我需要为 JWT 不记名令牌设置时钟偏差。但无论我做什么,代码都会忽略我的设置。

我尝试在基础设施层的依赖注入文件的多个不同部分中配置它,但它忽略了所有部分。

我现在有这个:

services.AddIdentityServer(options =>
  {
     options.IssuerUri = configuration.GetSection("MyCurrentDomainName")?.Value;
     options.Authentication.CookieLifetime = TimeSpan.FromDays(999);
     options.Authentication.CookieSlidingExpiration = true;

  }).AddApiAuthorization<ApplicationUser, ApplicationDbContext>();


services.AddAuthentication()
        .AddIdentityServerJwt();

services.Configure<JwtBearerOptions>(configuration =>
{
    configuration.TokenValidationParameters.ClockSkew = TimeSpan.FromSeconds(9875664);
});

services.TryAddEnumerable(ServiceDescriptor
    .Singleton<IConfigureOptions<JwtBearerOptions>, ConfigureBearerOptions>());

Services.AddTransient<IProfileService, ProfileService>();


services.TryAddEnumerable(ServiceDescriptor
   .Singleton<IPostConfigureOptions<JwtBearerOptions>, ConfigureJwtBearerOptions>());

代码忽略我的配置并将时钟偏差设置为默认的 5 分钟。

在上面代码的最后一行,我有一个 PostConfigurationOption,我也在那里设置了时钟偏差,它运行代码,但是当 WebUI 层调用身份验证时,时钟偏差将默认为 5分钟。

我在这里做错了什么?

【问题讨论】:

    标签: asp.net-core jwt


    【解决方案1】:
    services.AddIdentityServer(options =>
      {
         options.Authentication.CookieLifetime = TimeSpan.FromDays(999);
      });
    

    如果你使用 Jwt 令牌,为什么要设置 cookie 生命周期?

    services.Configure<JwtBearerOptions>(configuration =>
    {
        configuration.TokenValidationParameters.ClockSkew = TimeSpan.FromSeconds(9875664);
    });
    

    JwtBearerOptions 在应用服务中注册,但在内部注册。身份服务器不使用这个。

    身份服务器在内部使用服务器上的表Clients 来调整我们注册的每个客户端的时钟。对应列IdentityTokenLifetime, AccessTokenLifetime, AuthorizationCodeLifetime, ConsentLifetime, AbsoluteRefreshTokenLifetime, SlidingRefreshTokenLifetime指定的时间,根据需要修改。

    更新

    模板仍然使用IdentityServer4,它没有自己的魔力。这就是模板中发生的事情。

    在启动中,服务名为AddInfrastructure。这让应用程序可以使用内存数据库(作为 appSettings 上的默认配置),此外,IdentityServer 将appSettings 上的IdentityServerSPA 选项作为其配置文件。这将在.AddApiAuthorization&lt;ApplicationUser, ApplicationDbContext&gt;() 获得配置。

    因为它是一个预配置配置文件,所以你不能乱用它。

    要获得最清晰的说明,请使用 localdb sql server。我认为这样做的方法在Infrastructure 层上的DependencyInjection 文件中很清楚。然后我们会看到表格神奇地出现在sql上。

    如果只是一个简单的演示,试试这个:

    // Modify this block code in DependencyInjection file, Infrastructure layer
    services.AddIdentityServer()
                    .AddApiAuthorization<ApplicationUser, ApplicationDbContext>(cfg =>
                    {
                        var defaultClient = cfg.Clients["CleanArchitecture.WebUI"];
                        defaultClient.AccessTokenLifetime = 3600; // 3600s
                    });
    

    每个客户端的每种令牌的其他生命周期也可以配置。

    【讨论】:

    • 我正在使用 Jason Taylor 的 Clean Architecture 模板:github.com/jasontaylordev/CleanArchitecture。没有配置客户端的客户端表或 appsettings 文件。如何找到这些设置的来源?
    • 顺便感谢您的回复
    • 没关系,模板使用Microsoft.AspNetCore.ApiAuthorization.IdentityServer,它有IdentityServer4作为它的依赖......相信我,它确实使用Clients表。但在你的情况下,这真是个倒霉……请看看我更新的部分。
    猜你喜欢
    • 2021-06-27
    • 2019-02-22
    • 2017-04-21
    • 2019-10-14
    • 1970-01-01
    • 2018-09-15
    • 2020-08-22
    • 2019-07-29
    • 1970-01-01
    相关资源
    最近更新 更多