security - 我可以在 TokenValidationParameters 的哪里指定 JWT token 中的算法应该匹配 HS256?

标签 security asp.net-core .net-core jwt

我正在使用 AspNet Core 构建 Web API 和 JWT token 来对用户进行身份验证。

我在哪里可以在 TokenValidationParameters 中指定 token 中的 alg 应该匹配 HS256(永远不要 none) ?

    services
        .AddAuthentication(options =>
        {
            options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
            options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
            options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
        })
        .AddJwtBearer(cfg =>
        {
            cfg.RequireHttpsMetadata = false;
            cfg.SaveToken = true;
            string jwtIssuer = configuration["JwtIssuer"];
            SymmetricSecurityKey securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(configuration["JwtKey"]));
            cfg.TokenValidationParameters = new TokenValidationParameters
            {
                ValidIssuer = jwtIssuer,
                ValidAudience = jwtIssuer,
                ValidateIssuerSigningKey = true,
                IssuerSigningKey = securityKey,
                ClockSkew = TimeSpan.Zero
            };
        });

默认情况下,库是否拒绝将 alg 设置为 none 的传入 token ?

最佳答案

根据comment默认情况下,中间件不允许 "alg": "none"。似乎是由于 question 中提到的漏洞造成的.

关于拒绝:我尝试使用 "alg": "none" 传递 token 并在响应中得到 invalid_token (Microsoft.AspNetCore .Authentication.JwtBearer 2.1.0).

顺便说一句,规范requires HS256"none" 算法的实现。

关于security - 我可以在 TokenValidationParameters 的哪里指定 JWT token 中的算法应该匹配 HS256?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53403569/

相关文章:

apache - 内容安全策略 : allowing all external images?

symfony - 使用基本 http 身份验证的 REST 单元测试始终为 401 Unauthorized

c# - .net 核心包中缺少 ProtoBuf 格式化程序

c# - 微软依赖注入(inject)框架中Autofac的聚合服务相当于什么

angular - Angular 客户端是否应该将用户角色存储在本地存储中?

c# - 在 .NET 4.5.2 控制台应用程序中使用 .NET Core 库

c++ - localtime 和 asctime 不安全,但安全函数没有相同的参数

sql - 允许安全执行任意sql所需的权限

c# - FluentValidation 使用手动验证的隐式子验证

.net - 知道如何将 UDT 与 .NET Core 和 oracle 客户端一起使用吗?