A request to a protected page answers 302 Found with
Location: /Account/Login?ReturnUrl=... and curl, following the redirect, then got a
404 Not Found: cookie authentication sends anonymous users to its default login
path, and the app has no page there. Set LoginPath to the sign-in page the app
really has.
One line in AddCookie, options.LoginPath = "/sign-in" (or whatever
your page is called), turned the 404 into the sign-in page in the test. Mapping a page at
/Account/Login is the other way out; it was not tested here.
The error
PS> curl.exe -s -i -L -H "Accept: text/html" http://127.0.0.1:15610/reports | Select-String -Pattern '^HTTP|^Location'
HTTP/1.1 302 Found
Location: http://127.0.0.1:15610/Account/Login?ReturnUrl=%2Freports
HTTP/1.1 404 Not Found
These are the status lines and the Location header of the two responses, with
curl following the redirect (-L), kept by the Select-String pipe on the command line.
Why it happens
...
app.MapGet("/reports", () => Results.Content("<h1>Reports</h1>", "text/html")).RequireAuthorization();
...
app.MapGet("/sign-in", () => Results.Content("<h1>Sign in</h1>", "text/html"));
...
/reports requires an authenticated user, the request had no cookie, and cookie
authentication answered with a redirect to /Account/Login, the path it uses
when LoginPath is not set, with the original path in ReturnUrl.
The app's sign-in page was /sign-in; nothing was mapped at
/Account/Login, so the second request got 404. The path name in the
Location header is the clue: it is not one the app defines.
The fix
...
if (builder.Configuration["LoginPath"] is string loginPath)
{
options.LoginPath = loginPath;
}
...
started pid 39716: dotnet bin\Debug\net10.0\AfLab.dll --urls http://127.0.0.1:15610 --environment Development --Keys keys-a --LoginPath /sign-in --ApiStatus true
PS> curl.exe -s -i -L -H "Accept: text/html" http://127.0.0.1:15610/reports | Select-String -Pattern '^HTTP|^Location|<h1>'
HTTP/1.1 302 Found
Location: http://127.0.0.1:15610/sign-in?ReturnUrl=%2Freports
HTTP/1.1 200 OK
<h1>Sign in</h1>
The lab reads the path from a command-line setting; the switch was added and the app rebuilt between the first failing run and the fixed one, and the commands shown were rerun later from that one build, started the same two ways. In an app it
is the plain assignment options.LoginPath = "/sign-in"; inside
AddCookie. With it, the redirect pointed at /sign-in, kept the
ReturnUrl, and the page answered 200. The same run also had the
ApiStatus switch on, which only changes /api paths (see the post on
API endpoints below). AfLab, port 15610, the routes /reports, /sign-in and /api, and the switches are the test's own.
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 on 127.0.0.1:15610, with
AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie(...), AddAuthorization(), a
/reports page endpoint with RequireAuthorization() and a
/sign-in page. Requests went through curl 8.22.0 without a cookie.
Frequently asked
- Why does ASP.NET Core redirect to /Account/Login?
- In the test, cookie authentication without a LoginPath sent anonymous requests for a protected page to /Account/Login with the original path in ReturnUrl.
- How do I change the login URL for cookie authentication?
- Set options.LoginPath in AddCookie to your sign-in page. In the test LoginPath /sign-in changed the redirect to /sign-in?ReturnUrl=%2Freports and the page answered 200.
- Why is /Account/Login?ReturnUrl a 404?
- Because the app has no page at that path. In the test only /sign-in existed, so the redirect to the default /Account/Login ended in 404 Not Found.
More decoded errors in the Fixes category. For what the same redirect does to a JSON endpoint, see cookie authentication answers an API call with a 302.