【发布时间】:2021-06-06 01:54:59
【问题描述】:
如标题所示,jwtSecurityTokenHandler.WriteToken(jwt) 函数抛出 IDX10653 错误。完全例外。
System.ArgumentOutOfRangeException: 'IDX10653:
The encryption algorithm 'System.String' requires a key size of at least 'System.Int32' bits.
Key 'Microsoft.IdentityModel.Tokens.SymmetricSecurityKey', is of size: 'System.Int32'. Arg_ParamName_Name
我的示例用法如下:
public AccessToken CreateToken(User user, List<OperationClaim> operationClaims)
{
_accessTokenExpiration = DateTime.Now.AddMinutes(_tokenOptions.AccessTokenExpiration);
var securityKey = SecurityKeyHelper.CreateSecurityKey(_tokenOptions.SecurityKey);
var signingCredentials = SigningCredentialsHelper.CreateSigningCredentials(securityKey);
var jwt = CreateJwtSecurityToken(_tokenOptions, user, signingCredentials, operationClaims);
var jwtSecurityTokenHandler = new JwtSecurityTokenHandler();
var token = jwtSecurityTokenHandler.WriteToken(jwt);
return new AccessToken
{
Token = token,
Expiration = _accessTokenExpiration
};
}
我控制了我的每一个变量和函数。我认为没有错,我没有找到任何解决此错误代码的方法。有什么解决办法吗?
编辑:在内部细节中有额外的信息 ->
密钥“Microsoft.IdentityModel.Tokens.SymmetricSecurityKey”的大小为:“System.Int32”。 (参数'key')
我的 SecurityKey 创建函数如下:
public static SecurityKey CreateSecurityKey(string securityKey)
{
return new SymmetricSecurityKey(Encoding.UTF8.GetBytes(securityKey));
}
【问题讨论】: