System.ArgumentNullException: Value cannot be null. (Parameter 's') from
Encoding.UTF8.GetBytes(builder.Configuration["Jwt:Key"]) means the key came
back null. When dotnet user-secrets list shows the key and the app still
crashes, check the environment: in this test the app read user-secrets as Development and
saw nothing as Production or Staging. Give the other environments the value another way,
for example an environment variable named Jwt__Key.
The double underscore stands for the colon in Jwt:Key. With that variable
set, the same build that crashed in Production read a 45-byte key and started.
The error
dotnet user-secrets list
Jwt:Key = fix11-test-key-not-a-real-secret-0123456789
exit=0
dotnet run --no-build --no-launch-profile -e ASPNETCORE_ENVIRONMENT=Production
Environment: Production
Unhandled exception. System.ArgumentNullException: Value cannot be null. (Parameter 's')
at System.ArgumentNullException.Throw(String paramName)
at System.Text.Encoding.GetBytes(String s)
at Program.<Main>$(String[] args) in C:\...\Sec1\Program.cs:line 6
exit=-532462766
The path is shortened; nothing else changed. The key value is a made-up string for the
test. The (Parameter 's') is the name of the argument of
Encoding.GetBytes(string s), not a setting of yours.
Why it happens
The secret store is real and holds the value, but the app is not reading it. These are
the runs of one build, changing only ASPNETCORE_ENVIRONMENT:
Production and Staging crashed at line 6, and
Development printed Signing key: 43 bytes. So in this test
WebApplication.CreateBuilder added the user-secrets store for Development
only. A fifth run with no -e and no launch profile printed
Environment: Production and crashed the same way.
The compiler warned about this line before it ever ran:
warning CS8604: Possible null reference argument for parameter 's' in 'byte[]
Encoding.GetBytes(string s)'. builder.Configuration["Jwt:Key"] returns
string?, and in these runs it returned null for Jwt:Key, the one
key tested, whenever no loaded source held it.
The fix
set Jwt__Key=fix11-production-test-key-not-real-9876543210& dotnet run --no-build --no-launch-profile -e ASPNETCORE_ENVIRONMENT=Production
Environment: Production
Signing key: 45 bytes
exit=0
The line ran through cmd. The PowerShell form worked the same way:
PS> $env:Jwt__Key = 'fix11-production-test-key-not-real-9876543210'
PS> dotnet run --no-build --no-launch-profile -e ASPNETCORE_ENVIRONMENT=Production
Environment: Production
Signing key: 45 bytes
exit=0
Passing it with dotnet run -e Jwt__Key=... also worked here. On a server the
variable goes wherever that host sets environment variables for the process; no Linux
host was run for this post. Keep user-secrets for your own machine in Development, and
give Production its key from the host. The program used for the test:
using System.Text;
var builder = WebApplication.CreateBuilder(args);
Console.WriteLine($"Environment: {builder.Environment.EnvironmentName}");
var key = Encoding.UTF8.GetBytes(builder.Configuration["Jwt:Key"]);
Console.WriteLine($"Signing key: {key.Length} bytes");
...
How it was reproduced
A project from dotnet new web -f net10.0 with SDK 11.0.100 on Windows 11,
no packages added, dotnet user-secrets init and dotnet user-secrets
set "Jwt:Key" ... with a test value. The program read the key at start-up and
exited before serving. It was built once and run with
--no-launch-profile for Production, Staging, Development, no environment
name, and Production with Jwt__Key set.
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; the two PS> lines were typed in PowerShell.
Frequently asked
- Why are user secrets not loaded in Production?
- In this test an app built with WebApplication.CreateBuilder read the user-secrets store when ASPNETCORE_ENVIRONMENT was Development and returned null for the same key under Production and Staging. Production needs the value from another source, such as an environment variable.
- How do I set Jwt:Key as an environment variable?
- Replace the colon with a double underscore: Jwt__Key. In this test set Jwt__Key=value in cmd, $env:Jwt__Key = 'value' in PowerShell and dotnet run -e Jwt__Key=value all worked. Linux forms were not run for this post.
- What does Parameter 's' mean in Value cannot be null?
- It is the parameter name of Encoding.GetBytes(string s). The configuration lookup returned null and GetBytes refused it, so the real problem is the missing configuration value.
More decoded errors in the Fixes category. If
dotnet user-secrets set itself fails, see
Could
not find the global property 'UserSecretsId'.