【问题标题】:Asp.Net Core Jwt access_token signing and encryptionAsp.Net Core Jwt access_token 签名和加密
【发布时间】:2021-03-07 01:46:24
【问题描述】:

TLDR;我正在尝试了解 如何 Jwt 中间件能够识别我已使用以下签名/加密方案对我的令牌进行签名和加密:

SigningCredentials TokenSigningKey = new SigningCredentials( "MySignatureSecurityKey", SecurityAlgorithms.HmacSha512);
EncryptingCredentials TokenEncryptingKey = new EncryptingCredentials( "MyEncryptionSecurityKey", JwtConstants.DirectKeyUseAlg, SecurityAlgorithms.Aes256CbcHmacSha512 );

在我的Startup.csConfigureServices 方法中,我已将我的服务配置为AddAuthentication(options).AddJwtBearer:

services.AddAuthentication( options =>
{
    ...
} )
.AddJwtBearer( options =>
{
    ...
    options.TokenValidationParameters =
        new TokenValidationParameters
        {
            ...
            IssuerSigningKey = "MySignatureSecurityKey"
            TokenDecryptionKey = "MyEncryptionSecurityKey"
            ...
        };
} );

我不需要在任何地方配置签名/加密方案,我只是将要使用的密钥传递给它。我只在实际对令牌进行签名/加密时才配置签名/加密方案,并且它与中间件之间没有连接。

此外,任何关于这是否正确定义或在哪里可以找到有关当前最佳实践配置的文档的建议将不胜感激:

SigningCredentials TokenSigningKey = new SigningCredentials( "MySignatureSecurityKey", SecurityAlgorithms.HmacSha512);
EncryptingCredentials TokenEncryptingKey = new EncryptingCredentials( "MyEncryptionSecurityKey", JwtConstants.DirectKeyUseAlg, SecurityAlgorithms.Aes256CbcHmacSha512 );

由于文档非常粗糙,我不得不从网络上的各种零碎拼凑起来。

【问题讨论】:

    标签: encryption asp.net-core-3.1 jwt-auth c#-8.0


    【解决方案1】:

    天哪,真不敢相信我错过了。在我应用加密后检查它时,我一定是错误地解码了 access_token,因为我刚刚又做了一次,答案直盯着我。

    access_token 的第一部分如下所示: eyJhbGciOiJkaXIiLCJlbmMiOiJBMjU2Q0JDLUhTNTEyIiwidHlwIjoiSldUIn0.. 解码后,它看起来像这样: {"alg":"dir","enc":"A256CBC-HS512","typ":"JWT"}

    总荷马辛普森矩 XS

    所以回答这个问题: Jwt 中间件获取加密的令牌(它以bearer token 的形式呈现)并通过解码第一部分(见上文),它可以看到以下内容:

    1. 算法 = "dir" / DirectKeyUse
    2. 加密 =“A256CBC-HS512”/Aes256CbcHmacSha512

    中间件知道处理它,因为“typ”是“JWT”。

    希望这对其他人也有帮助。

    【讨论】:

      猜你喜欢
      • 2017-02-27
      • 2015-11-23
      • 2016-06-17
      • 1970-01-01
      • 2020-05-15
      • 2019-05-21
      • 1970-01-01
      • 2016-11-23
      • 2018-07-26
      相关资源
      最近更新 更多