The antiforgery system has the configuration value
AntiforgeryOptions.Cookie.SecurePolicy = Always, but the current request is not an SSL
request. means antiforgery was told to issue its cookie only over HTTPS and a page
with a form was requested over plain http. Serve the form over HTTPS. Leaving
SecurePolicy at its default also removes the error, but then the antiforgery
cookie is no longer restricted to HTTPS.
The setting worked in the test once the site ran on HTTPS. The 500 came when the same app was reached over an http address.
The error
started pid 38056: dotnet bin\Debug\net10.0\AfLab.dll --urls http://127.0.0.1:15610 --environment Development --Keys keys-a --AfSecure true
PS> curl.exe -s -i -c jar.txt -H "Accept: text/plain" http://127.0.0.1:15610/form | Select-Object -First 16
HTTP/1.1 500 Internal Server Error
Content-Type: text/plain; charset=utf-8
...
System.InvalidOperationException: The antiforgery system has the configuration value AntiforgeryOptions.Cookie.SecurePolicy = Always, but the current request is not an SSL request.
at Microsoft.AspNetCore.Antiforgery.DefaultAntiforgery.CheckSSLConfig(HttpContext context)
at Microsoft.AspNetCore.Antiforgery.DefaultAntiforgery.GetAndStoreTokens(HttpContext httpContext)
...
This is the response body of GET /form in the Development environment; the
request sent Accept: text/plain and the body was this text. The server console
logged the same exception under
fail: Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1].
It came from the GET that renders the form, before any POST.
Why it happens
The test app registered antiforgery with
options.Cookie.SecurePolicy = CookieSecurePolicy.Always and was started on
http://127.0.0.1:15610. The form endpoint calls
IAntiforgery.GetAndStoreTokens, and the stack trace shows that call throwing
on the http request instead of handing out a token and its cookie.
The fix
started pid 38604: dotnet bin\Debug\net10.0\AfLab.dll --urls http://127.0.0.1:15610 --environment Development --Keys keys-a --AfSecure true --urls https://127.0.0.1:15613
...
PS> curl.exe -s -i -c jar.txt https://127.0.0.1:15613/form
HTTP/1.1 200 OK
...
Set-Cookie: .AspNetCore.Antiforgery.0p-kydK_Bmc=CfDJ8...; path=/; secure; samesite=strict; httponly
...
The same app, the same SecurePolicy, started with a second --urls (in this
run the console listed only Now listening on: https://127.0.0.1:15613): the form
page answered 200, the antiforgery cookie carried secure, and posting the form
with that cookie returned "Booked for Maria Garcia.". Kestrel used its default certificate
on this PC, and curl needed no option to accept it.
With SecurePolicy left at its default, the same app answered 200 for
/form over http. That removes the error, but the antiforgery cookie is then no
longer restricted to HTTPS (its http Set-Cookie header was not captured here), so serving the form over HTTPS is the better
fix. The AfSecure switch is the test's own way to turn the setting on.
How it was reproduced
A project from dotnet new web -n AfLab -f net10.0 on .NET SDK 11.0.100, no
NuGet packages, running on ASP.NET Core 10.0.15 in Development: a GET /form
endpoint that writes the hidden field from IAntiforgery.GetAndStoreTokens, a
POST /book endpoint with [FromForm] string name,
app.UseAntiforgery(), and AddAntiforgery with the secure policy
behind a command-line setting. Requests went through curl 8.22.0 (Schannel) on 127.0.0.1
ports 15610 (http) and 15613 (https).
Frequently asked
- What does antiforgery SecurePolicy Always but the current request is not an SSL request mean?
- Antiforgery was configured to send its cookie over HTTPS only, and a page that renders a token was requested over http. In the test GET /form answered 500 with this exception.
- How do I fix the current request is not an SSL request in ASP.NET Core?
- Serve the form over HTTPS: in the test the same app on an https address answered 200 and the form posted normally. Leaving AntiforgeryOptions.Cookie.SecurePolicy at its default also removes the error, at the cost of a cookie that is no longer restricted to HTTPS.
- Does this antiforgery error happen on the POST or the GET?
- In the test it happened on the GET that renders the form, inside GetAndStoreTokens, before any form was posted.
More decoded errors in the Fixes category. For HTTPS on a development machine, see the HTTPS developer certificate.