首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >无法验证RS256签名的JWT

无法验证RS256签名的JWT
EN

Stack Overflow用户
提问于 2020-11-19 02:09:35
回答 1查看 88关注 0票数 1

我正在尝试和KeyCloak一起在dotnet webapi上实现签名的JWT (RS256)。在app start上,我可以看到使用预期的响应内容(如下所示的请求)对keycloak进行的openid调用。

在这里获取jwks_url

代码语言:javascript
运行
复制
GET https://localhost:8080/auth/realms/core/.well-known/openid-configuration

从这里拿钥匙

代码语言:javascript
运行
复制
GET https://localhost:8080/auth/realms/core/protocol/openid-connect/certs

然后,我得到一个带有以下请求的access_token

代码语言:javascript
运行
复制
POST https://localhost:8080/auth/realms/core/protocol/openid-connect/token
Content-Type: application/x-www-form-urlencoded

grant_type=password&client_id=admin-cli&username=jim&password=foobar

然后,我测试了以下端点

代码语言:javascript
运行
复制
[ApiController]
[Route("/")]
public class AppController : ControllerBase
{
    [Authorize]
    [HttpGet]
    public OkObjectResult Get()
    {
        return Ok("This is the secured page");
    }
}

使用此请求

代码语言:javascript
运行
复制
GET https://localhost:5001
Authorization: Bearer MY_TOKEN 

但我总是得401分

代码语言:javascript
运行
复制
HTTP/1.1 401 Unauthorized
Content-Length: 0
Date: Wed, 18 Nov 2020 17:41:28 GMT
Server: Kestrel
Www-Authenticate: Bearer error="invalid_token", error_description="The signature key was not found"

令牌中确实存在签名密钥(第三个“块”)。下面是JWT验证代码。我是不是遗漏了什么?

代码语言:javascript
运行
复制
public void ConfigureServices(IServiceCollection services)
{
    services.AddControllers();

    var audience = Configuration["Jwt:Audience"];
    var issuer = Configuration["Jwt:Issuer"];
    bool.TryParse(Configuration["Jwt:RequireHttpsMetadata"], out var requireHttpsMetadata);

    IConfigurationManager<OpenIdConnectConfiguration> configurationManager =
        new ConfigurationManager<OpenIdConnectConfiguration>(
            $"{Configuration["Jwt:Authority"]}/auth/realms/core/.well-known/openid-configuration",
            new OpenIdConnectConfigurationRetriever());
    var openIdConfig =
        configurationManager.GetConfigurationAsync(CancellationToken.None).Result;

    services.AddAuthentication(options =>
        {
            options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
            options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
            options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
        })
        .AddJwtBearer(options =>
        {
            options.SaveToken = true;
            options.RequireHttpsMetadata = requireHttpsMetadata;
            options.TokenValidationParameters.IssuerSigningKeys = openIdConfig.SigningKeys;
            options.TokenValidationParameters = new TokenValidationParameters
            {
                ValidateIssuer = true,
                ValidateAudience = true,
                ValidateLifetime = true,

                ValidIssuer = issuer,
                ValidAudience = audience,
                ValidateIssuerSigningKey = true,
            };
        });
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-11-19 03:32:35

因为JWT拥有一个签名(也就是第三个块),所以我将解释消息

代码语言:javascript
运行
复制
"The signature key was not found"

签名的验证有问题。

重新检查响应/可访问性

代码语言:javascript
运行
复制
GET https://localhost:8080/auth/realms/core/protocol/openid-connect/certs
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64898856

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档