Error message
IDX10720: Unable to create KeyedHashAlgorithm for algorithm 'HS256', the key size must be greater than: '256' bits, key has '128' bits.
Also printed as
- System.ArgumentOutOfRangeException: IDX10720: Unable to create KeyedHashAlgorithm for algorithm 'HS256', the key size must be greater than: '256' bits, key has '128' bits. (Parameter 'keyBytes')
- IDX10653: The encryption algorithm 'HS256' requires a key size of at least '128' bits.
Algorithm and bit counts reflect your configuration; despite "must be greater than", a key of exactly the required size is accepted. Checked across versions: 6.8.0 and earlier accepted 128-bit HMAC keys; later 6.x (6.36.0) and 7.x/8.x reject them with IDX10720 (6.x adds a link to "UnsafeRelaxHmacKeySizeValidation"). Keys under 128 bits fail earlier with IDX10653 "The encryption algorithm 'HS256' requires a key size of at least '128' bits".
Microsoft.IdentityModel.Tokens (.NET) — ArgumentOutOfRangeException from SymmetricSignatureProvider
What it means
IdentityModel refuses to use an HMAC key shorter than the hash output of the algorithm: HS256 needs at least 256 bits (32 bytes), HS384 384, HS512 512, as RFC 7518 §3.2 requires. The key in the message has 128 bits — 16 bytes, a typical length of a hand-typed secret in `appsettings.json`. Short HMAC secrets can be brute-forced offline from a single token, which would let anyone mint tokens your API accepts.
Why it happens
A short secret in configuration
commonTutorials use values like `MySecretKey12345` or `supersecret`. Their UTF-8 bytes are far below 32.
Upgrading Microsoft.IdentityModel past early 6.x
commonReleases up to at least 6.8.0 signed with 128-bit HMAC keys; current 6.x, 7.x and 8.x enforce the RFC minimum, so an upgrade (often pulled in by an ASP.NET Core upgrade) breaks applications with short secrets.
HS512 with a key sized for HS256
occasionalA 32-byte key is enough for HS256 but not for HS512, which needs 64 bytes.
How to fix it
- 1.
Generate a random key of the right size
Use a cryptographically random value, stored in user secrets or a vault, never committed to source control.
bash # 64 random bytes, base64: valid for HS256, HS384 and HS512 openssl rand -base64 64 | tr -d '\n' dotnet user-secrets set "Jwt:KeyBase64" "<the value above>" # development; use a vault in production - 2.
Decode it to bytes
Build the key from the decoded bytes so the full entropy is used and every service derives the same key.
csharp var keyBytes = Convert.FromBase64String(builder.Configuration["Jwt:KeyBase64"]!); var signingKey = new SymmetricSecurityKey(keyBytes); // 64 bytes