OptionsValidationException: DataAnnotation validation failed for 'JwtOptions'
members: 'Key' with the error: 'The Key field is required.' is the check you asked
for doing its job: ValidateOnStart() found no value for Jwt:Key
in any configuration source, so the host refused to start. Supply the value for the
environment the app runs in (here an environment variable Jwt__Key) and the
same build starts.
Do not remove the validation to make the message go away: it is what stopped the app before it could accept a single request without a signing key.
The error
dotnet run --no-build --no-launch-profile --urls http://127.0.0.1:15640
fail: Microsoft.Extensions.Hosting.Internal.Host[11]
Hosting failed to start
Microsoft.Extensions.Options.OptionsValidationException: DataAnnotation validation failed for 'JwtOptions' members: 'Key' with the error: 'The Key field is required.'.
at Microsoft.Extensions.Options.OptionsFactory`1.Create(String name)
...
at Microsoft.Extensions.Options.StartupValidator.Validate()
at Microsoft.Extensions.Hosting.Internal.Host.StartAsync(CancellationToken cancellationToken)
Unhandled exception. Microsoft.Extensions.Options.OptionsValidationException: DataAnnotation validation failed for 'JwtOptions' members: 'Key' with the error: 'The Key field is required.'.
...
exit=-532462766
It appears twice in the console: once logged by the host as Hosting failed to
start, once as the unhandled exception that ends the process.
Why it happens
The options class marks Key as [Required], and the section
Jwt in appsettings.json held only the issuer:
...
builder.Services.AddOptions<JwtOptions>()
.BindConfiguration("Jwt")
.ValidateDataAnnotations()
.ValidateOnStart();
...
public class JwtOptions
{
[Required]
public string Key { get; set; } = "";
[Required]
public string Issuer { get; set; } = "";
}
...
"Jwt": {
"Issuer": "fix11-test-issuer"
}
}
With nothing bound, Key kept its default empty string, and
[Required] rejected it. The stack shows StartupValidator.Validate()
inside Host.StartAsync. The message names the member that failed,
'Key', and the attribute's own error text.
To see what ValidateOnStart() changes, a copy of the project ran without
it, with an endpoint /issuer that reads IOptions<JwtOptions>.
The host started, / answered 200, and the first request to the endpoint
that read the options failed:
curl.exe -s -i http://127.0.0.1:15642/issuer
HTTP/1.1 500 Internal Server Error
Content-Length: 0
...
exit=0
fail: Microsoft.AspNetCore.Server.Kestrel[13]
Connection id "0HNQ1TSHAM939", Request id "0HNQ1TSHAM939:00000001": An unhandled exception was thrown by the application.
Microsoft.Extensions.Options.OptionsValidationException: DataAnnotation validation failed for 'JwtOptions' members: 'Key' with the error: 'The Key field is required.'.
at Microsoft.Extensions.Options.OptionsFactory`1.Create(String name)
at Microsoft.Extensions.Options.UnnamedOptionsManager`1.get_Value()
...
The same message, but in the server console of a running app and as an empty 500 to the client, not at start-up.
The fix
set Jwt__Key=fix11-test-signing-key-not-real-0123456789abcdef& dotnet run --no-build --no-launch-profile --urls http://127.0.0.1:15640
info: Microsoft.Hosting.Lifetime[14]
Now listening on: http://127.0.0.1:15640
info: Microsoft.Hosting.Lifetime[0]
Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
Hosting environment: Production
Started; stopping (test run).
...
Jwt__Key is Jwt:Key in environment-variable form, and it bound
to JwtOptions.Key without any code change. The key is a made-up string for
the test. A key belongs in the environment, a secret store or user-secrets on a developer
machine, not in appsettings.json in the repository. The issuer, which is not
a secret, stayed in the JSON file in this run.
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 options and DataAnnotations validation come with the ASP.NET Core
shared framework). The program above, an appsettings.json with only
Jwt:Issuer, run with --no-launch-profile on 127.0.0.1:15640.
For the test the program started the host and stopped it again instead of serving. The
copy without ValidateOnStart() served on 127.0.0.1:15642, was asked with
curl.exe and was stopped by its process id.
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
- How do I fix DataAnnotation validation failed for members with the error field is required?
- Give the named member a value from a source the app reads in its current environment. In this test the environment variable Jwt__Key supplied the missing Key and the same build started.
- What does ValidateOnStart do in ASP.NET Core?
- In this test, with ValidateOnStart the missing key stopped the host at start-up with OptionsValidationException. Without it the app started, and the first request to an endpoint that read the options got a 500 with the same exception in the server console.
- Why does Required fail when my property has a default of empty string?
- In this test the Key property was initialised to an empty string, nothing in configuration replaced it, and the Required attribute reported The Key field is required. A non-empty value from configuration made the check pass.
More decoded errors in the Fixes category. If the key is in user-secrets and the app still does not see it, read why user-secrets were not loaded in Production.