System.FormatException: The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters. from Convert.FromBase64String on a signing key means the key in configuration is plain text while the code expects Base64. Generate a random key, store its Base64 form, and the same code reads it.

In the test a 44-character Base64 value made by Convert.ToBase64String(RandomNumberGenerator.GetBytes(32)) decoded to a 32-byte key, where the text keys had thrown at start-up.

The error

set Jwt__Key=fix11-test-signing-key-not-real-0123456789abcdef& dotnet run --no-build --no-launch-profile
Jwt:Key is 48 characters
Unhandled exception. System.FormatException: The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters.
   at System.Convert.FromBase64CharPtr(Char* inputPtr, Int32 inputLength)
   at System.Convert.FromBase64String(String s)
   at Program.<Main>$(String[] args) in C:\...\fixes11\cfg\B64w\Program.cs:line 5
exit=-532462766

The path is shortened; nothing else changed. Every key on this page is a test value made for the run, not a real secret.

Why it happens

The program decodes the configured key:

var builder = WebApplication.CreateBuilder(args);

var keyText = builder.Configuration["Jwt:Key"]!;
Console.WriteLine($"Jwt:Key is {keyText.Length} characters");
var keyBytes = Convert.FromBase64String(keyText);
Console.WriteLine($"Signing key: {keyBytes.Length} bytes");
...

Base64 uses letters, digits, +, / and = padding, in groups of four characters. The first test key has hyphens, which are outside that set. A second key with letters and digits only, fix11testsigningkeynotreal, threw the same message because 26 characters are not a whole number of groups.

A third run is the one to worry about. fix11testsigningkeynotrealab, 28 characters, decoded without any error:

set Jwt__Key=fix11testsigningkeynotrealab& dotnet run --no-build --no-launch-profile
Jwt:Key is 28 characters
Signing key: 21 bytes
exit=0

A plain-text key that happens to fit the Base64 alphabet and length is accepted and turned into different bytes than the text suggests. Whatever issues or checks tokens must decode the key the same way, so pick one form and store that form everywhere.

The fix

Generate a key once, with a small console program:

using System.Security.Cryptography;

// Prints a new random 32-byte key as Base64 (test only).
Console.WriteLine(Convert.ToBase64String(RandomNumberGenerator.GetBytes(32)));
dotnet run --no-build --project ..\Gen
Wctpn5PSkTVzI3Wn+jBSlE6pBwYyjfr43DfXCgQLZxE=
exit=0

Then the same app with that value:

set Jwt__Key=Wctpn5PSkTVzI3Wn+jBSlE6pBwYyjfr43DfXCgQLZxE=& dotnet run --no-build --no-launch-profile
Jwt:Key is 44 characters
Signing key: 32 bytes
exit=0

The value shown was generated for this test and used nowhere else; make your own and keep it out of the repository (user-secrets in Development, the host's environment or a secret store elsewhere).

How it was reproduced

A project from dotnet new web -f net10.0 with SDK 11.0.100 on Windows 11, no packages added, the key read from the environment variable Jwt__Key set for each run in cmd. The program decoded the key at start-up, printed its size and exited. Four keys were tried: a text key with hyphens, a 26-character and a 28-character letters-only key, and a generated Base64 key from a second dotnet new console project. Every command shown without a PS> marker ran in cmd: a PowerShell script passed it as cmd /c ... 2>&1 and printed the exit= line after it.

Frequently asked

Why does Convert.FromBase64String say the input is not a valid Base-64 string?
The string has a character outside the Base64 set, such as a hyphen, or its length is not a whole number of four-character groups. In this test both a text key with hyphens and a 26-character letters-only key gave the same message.
How do I generate a Base64 signing key in C#?
Call Convert.ToBase64String(RandomNumberGenerator.GetBytes(32)) from System.Security.Cryptography. It prints a 44-character string that decodes back to 32 random bytes.
Can a plain text key pass Convert.FromBase64String?
Yes. In this test a 28-character key of letters only decoded without error into 21 bytes, so a text key can be silently read as different bytes. Store keys in one agreed form.

More decoded errors in the Fixes category. If the key is null rather than malformed, see Value cannot be null in Production.