签署令牌如何工作? (无效签名错误)

How does signing a token work? (Invalid Signature Error)

背景: 我正在 运行 .NET Core Web API 中进行集成测试,它使用 Auth0 在我的端点上进行授权。当我将令牌粘贴到 JWT.io 验证器时,我从 Auth0 返回的令牌包含“无效签名”。

我无法理解我的令牌在我的授权流程中的哪个点被签名、谁签名以及如何签名。

我在 Auth0(“My Dev API”)中开始了一项新开发 API,它使用 HS256 签名算法来签署我的令牌。据我了解,对于 HS256,有一个密钥用于签名令牌(签名密钥),机器到机器的流程如下所示:

我 post Auth0 的一些凭据,如下所示:

clientID: exampleID
clientSecret: exampleSecret
audience: https://myaudience.com
grant_type: client_credentials

Auth0 发回一个访问令牌:

access_token: exampleToken1234

当我将此令牌粘贴到 JWT.io 时,我被告知该令牌的签名无效。

在我的 .NET 应用程序中,我有一些设置进行某种配置:

   static void AddAuthentication(IServiceCollection services)
       {
           services.AddAuthentication(options =>
           {
               options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
               options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
    }).AddJwtBearer(options =>
        {
              options.TokenValidationParameters = new TokenValidationParameters
         {
               ValidIssuer = auth0Config.Domain,
               ValidAudience = auth0Config.Audience,
               IssuerSigningKey = new 
                        SymmetricSecurityKey(Encoding.UTF8.GetBytes(auth0Config.SigningSecret))
         };
     });
 }

任何人都可以解释我是否应该添加代码来签署这个令牌,如果它到达时已经签署(如果是这样,为什么 JWT.io 告诉我有一个无效的签名)以及上面的配置是什么究竟在做什么?

此外,是否需要创建一个独立的 Dev API 或者我可以将 autho 请求发送到预建管理-api?

提前致谢!

My tokens coming back from Auth0 contain an “invalid signature” when I paste them into the JWT.io verifier.

https://jwt.io 无法验证签名,除非您将秘密(用于签署令牌)粘贴到 "VERIFY SIGNATURE" 下右栏的字段中。所以首先将秘密粘贴到那里,然后将令牌粘贴到左栏中。的顺序很重要!

I’m having some trouble understanding at which point in my Authorization flow that my token is being signed, who signs it, and how it is signed.

AuthO 在根据提供的凭据对用户进行身份验证时为您创建一个签名令牌。要首先创建令牌 header 并创建有效载荷,然后将签名计算为 header 以及有效载荷和秘密的哈希值。阅读基础知识 here

Can anyone explain if I’m supposed to add code to sign this token, if it arrives already signed (and if so, why is JWT.io telling me there’s an invalid signature)

您不需要任何额外的签名,您会从 AuthO 获得一个签名令牌。

and what the config above is actually doing?

上面的配置告诉您 API 使用了基于 JWT 的身份验证,并且应该根据配置的参数(根据 auth0Config 的值验证发行者、受众和签名)检查传入请求是否为有效的 JWT )