【发布时间】: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