ASP.NET Core cannot create the JWT because the signing key is too short or unusable
IDX10720: Unable to create KeyedHashAlgorithm for algorithm 'HS256', the key size must be greater than: '<bits>' bits, key has '<bits>' bits.Short answer
The error happens while the API signs the token, not while it checks one. IdentityModel builds a signature provider from your key and algorithm and refuses when the HS256 secret is too short, when an RSA key has no private part, or when the algorithm name is not a supported one. The IDX code in the message tells which.
Fix it fast
1Find out which cause you have
Print what you are signing with: how many bits the key has and which algorithm name you pass. The IDX number in the exception tells you which of the causes below applies.
var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(secret)); Console.WriteLine($"key size: {key.KeySize} bits, algorithm: {SecurityAlgorithms.HmacSha256}");2Match what you see, then jump to its fix
- 1
IDX10720 or IDX10630, and the printed key size is well under 256 bits. A 16 character secret read as UTF-8 text gives only 128 bits.The HS256 secret is shorter than 32 bytesFix - 2
IDX10638 says key.HasPrivateKey is false.The RSA key has no private part, so nothing can be signedFix - 3
IDX10634 prints an Algorithm value and a SecurityKey and says that this combination is not supported.The algorithm string, or its pairing with the key, is not supportedFix
- 1
What does the error look like?
IDX10720: Unable to create KeyedHashAlgorithm for algorithm 'HS256', the key size must be greater than: '<bits>' bits, key has '<bits>' bits.IDX10630: The '<key type>' for signing cannot be smaller than '<bits>' bits. KeySize: '<bits>'.IDX10638: Cannot create the SignatureProvider, 'key.HasPrivateKey' is false, cannot create signatures.IDX10634: Unable to create the SignatureProvider.
Cause 1 of 3
The HS256 secret is shorter than 32 bytes
1Check
The JWA standard requires a key at least as large as the hash output for HMAC signatures, which is 256 bits for HS256. IdentityModel also has its own minimum key size for signing, and it throws when your key is below it.
Console.WriteLine($"{secret.Length} characters, {key.KeySize} bits");2Fix
Generate 32 or more random bytes, store them base64-encoded in your secret store or configuration, and decode them into the key. Use the same bytes wherever tokens are validated, otherwise tokens made with the old secret stop validating.
# one-time: create a 32-byte secret
openssl rand -base64 32
// C#: decode the stored value instead of encoding a phrase
var key = new SymmetricSecurityKey(Convert.FromBase64String(config["Jwt:Key"]!));
var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);3Verify
The printed size is 256 bits or more and the handler returns a token string.
Console.WriteLine(key.KeySize);
Console.WriteLine(new JwtSecurityTokenHandler().WriteToken(new JwtSecurityToken(signingCredentials: creds)));Confirmed by rfc-editor.org
Cause 2 of 3
The RSA key has no private part, so nothing can be signed
1Check
Signing needs the private key. A certificate loaded from a .cer file, or an RSA key built from public material only, carries just the public half. HasPrivateKey tells you which one you have.
var cert = X509CertificateLoader.LoadPkcs12FromFile("signing.pfx", pfxPassword);
Console.WriteLine(cert.HasPrivateKey);2Fix
Load a PFX (PKCS#12) file that contains the private key, and build the signing key from that certificate. Keep the public certificate for the services that only validate tokens.
var cert = X509CertificateLoader.LoadPkcs12FromFile("signing.pfx", pfxPassword);
var creds = new SigningCredentials(new X509SecurityKey(cert), SecurityAlgorithms.RsaSha256);3Verify
HasPrivateKey prints True and the token is written without IDX10638.
Console.WriteLine(cert.HasPrivateKey);Confirmed by learn.microsoft.com
Cause 3 of 3
The algorithm string, or its pairing with the key, is not supported
1Check
The message prints the algorithm string it received next to the key, and links to the list of supported algorithms. Compare your string with the names on that list, and check that it suits the kind of key you pass.
grep -rn "SigningCredentials(" --include=*.cs .2Fix
Pass one of the SecurityAlgorithms constants from the supported list, for example HmacSha256 for an HS256 token and RsaSha256 for RS256.
new SigningCredentials(key, SecurityAlgorithms.HmacSha256)
// for an RSA or certificate key:
// new SigningCredentials(rsaKey, SecurityAlgorithms.RsaSha256)3Verify
The exception no longer appears when you write the token.
var jwt = new JwtSecurityTokenHandler().WriteToken(token);Confirmed by github.com
How do I stop it coming back?
- Create signing secrets with a random generator and keep them out of source control, instead of typing a phrase.
- Check the key size at start-up and fail fast with a clear message when it is under 256 bits.
- Use the SecurityAlgorithms constants and never raw algorithm strings.
- Keep the signing key and the validation key in one shared setting so they can not drift apart.
Sources
- RFC 7518: JSON Web Algorithms (HS256 key size)
- IdentityModel source: LogMessages.cs
- Microsoft Learn: SymmetricSignatureProvider.DefaultMinimumSymmetricKeySizeInBits
- Microsoft Learn: Convert.FromBase64String
- Microsoft Learn: X509CertificateLoader.LoadPkcs12FromFile
- IdentityModel wiki: Supported algorithms
- OpenSSL: openssl-rand
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Security
.NET cannot convert the string to a JwtSecurityToken
SecurityTokenMalformedException
The handler only accepts a compact JWT: three dot-separated base64url segments for a signed token.
3 causes, each with a fixSecurity
AuthenticationException: your certificate validation callback returned false
System.Security.Authentication.AuthenticationException
This message means a validation callback was installed, yours or one a library added, and it returned false.
3 causes, each with a fixSecurity
DelegateAuthenticationProvider no longer exists in the Microsoft Graph .NET SDK
The type or namespace name 'DelegateAuthenticationProvider' could not be found
Microsoft Graph .NET SDK v5 removed DelegateAuthenticationProvider.
3 causes, each with a fixSecurity
A valid JWT fails to decode because its segments are base64url, not base64
Convert.FromBase64String throws FormatException on a JWT segment
A JWT is three base64url segments joined by dots, and a standard base64 decoder refuses all three differences: base64url uses - and _ instead of + and /, leaves off the = padding, and the dots are not part of any base64 alphabet.
3 causes, each with a fixSecurity
JWT validation fails and the message only says PII is hidden
401 Unauthorized
IdentityModel hides personal data in exception text by default, so the message only says PII is hidden.
3 causes, each with a fixSecurity
JwtBearer code stops working after moving an ASP.NET Core API to .NET 8
Claims read by name no longer match after upgrading to .NET 8
Since ASP.NET Core 8, JwtBearer events return a JsonWebToken instead of a JwtSecurityToken.
3 causes, each with a fix