SignInAsync when principal.Identity.IsAuthenticated is false is not allowed when AuthenticationOptions.RequireAuthenticatedSignIn is true. came, in this test, from a ClaimsIdentity, passed to SignInAsync, that was created without an authentication type. Pass the scheme name as the second argument of the ClaimsIdentity constructor.

The claims can be exactly right and the sign-in still fails: in the test a ClaimsIdentity with a name claim and no authentication type was refused. Switching RequireAuthenticatedSignIn off was not run here; the test kept it on.

The error

PS> curl.exe -s -i -H "Accept: text/plain" "http://127.0.0.1:15610/login-no-type?user=maria" | Select-Object -First 10
HTTP/1.1 500 Internal Server Error
Content-Type: text/plain; charset=utf-8
...

System.InvalidOperationException: SignInAsync when principal.Identity.IsAuthenticated is false is not allowed when AuthenticationOptions.RequireAuthenticatedSignIn is true.
   at Microsoft.AspNetCore.Authentication.AuthenticationService.SignInAsync(HttpContext context, String scheme, ClaimsPrincipal principal, AuthenticationProperties properties)
...

This is the response body of the sign-in endpoint in the Development environment; the request sent Accept: text/plain and the body was this text. The response headers shown carried no Set-Cookie.

Why it happens

The failing endpoint built its identity from one claim and nothing else:

...
    var identity = new ClaimsIdentity([new Claim(ClaimTypes.Name, user)]);
    await context.SignInAsync(new ClaimsPrincipal(identity));
...

The message says IsAuthenticated was false, and the only difference from the working sign-in below is the missing authentication type. The app used cookie authentication with RequireAuthenticatedSignIn at its default, and the call threw before any cookie was written. The exception names the setting that refused the call, which makes switching it off look like the fix; the test kept it on and changed the identity instead, because an identity that does not count as signed in is the actual mistake.

The fix

...
    var identity = new ClaimsIdentity([new Claim(ClaimTypes.Name, user)], CookieAuthenticationDefaults.AuthenticationScheme);
    await context.SignInAsync(new ClaimsPrincipal(identity));
...
PS> curl.exe -s -c jar2.txt "http://127.0.0.1:15610/login?user=maria"
Signed in as maria.
PS> curl.exe -s -i -b jar2.txt -H "Accept: application/json" http://127.0.0.1:15610/api/reports | Select-String -Pattern '^HTTP|count'
HTTP/1.1 200 OK
{"count":3}

The only difference is the second constructor argument, the scheme name CookieAuthenticationDefaults.AuthenticationScheme. The sign-in returned "Signed in as maria.", and the cookie it set opened /api/reports, an endpoint that requires an authenticated user. The second curl line keeps only the status line and the body, through its Select-String pipe; it was rerun in that form from the same build, started the same way. AfLab, port 15610, the routes /login, /login-no-type and /api/reports, the file jar2.txt and the user maria 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() and two test-only sign-in endpoints that take a user name and no password: one with the identity above, one with the scheme name added. Requests went through curl 8.22.0.

Frequently asked

Why is IsAuthenticated false on my ClaimsIdentity?
In the test, new ClaimsIdentity(claims) failed at SignInAsync and new ClaimsIdentity(claims, scheme) worked.
Should I set RequireAuthenticatedSignIn to false?
Not to fix this. The check caught an identity that would not count as signed in; giving the identity an authentication type is the fix the test confirmed.
What authentication type should a cookie sign-in use?
In the test the scheme name, CookieAuthenticationDefaults.AuthenticationScheme, was passed as the authentication type, and the cookie it produced was accepted by an endpoint that requires an authenticated user.

More decoded errors in the Fixes category. When no scheme is registered at all the message is a different one: No authenticationScheme was specified.