Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JWT WriterToken function throwing IDX10653 Error

Tags:

c#

jwt

.net-5

As the title, jwtSecurityTokenHandler.WriteToken(jwt) function is throwing IDX10653 Error. Full exception down there.

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

And my example usage is down there:

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
    };
}

I controlled my every variable and function. I think There is not a wrong thing and I don't find any solution for this error code. Any solution?

Edit: In the inner details an extra infos ->

Key 'Microsoft.IdentityModel.Tokens.SymmetricSecurityKey', is of size: 'System.Int32'. (Parameter 'key')

My SecurityKey creator function is like:

public static SecurityKey CreateSecurityKey(string securityKey)
{
    return new SymmetricSecurityKey(Encoding.UTF8.GetBytes(securityKey));
}
like image 414
Cagkan Mert Oztas Avatar asked Oct 31 '25 21:10

Cagkan Mert Oztas


1 Answers

That problem is solved.

The password ("SecurityKey") in the TokenOptions section in the appsettings.json file in the WebApi layer was short when working with the SHA512 encryption system and varbinary(500).

"TokenOptions": {
    "Audience": "[email protected]",
    "Issuer": "[email protected]",
    "AccessTokenExpiration": 10,
    "SecurityKey": "mykey"
  }
like image 83
Cagkan Mert Oztas Avatar answered Nov 02 '25 13:11

Cagkan Mert Oztas