I am trying to follow the instructions here to add Cookie Authentication to my site.
So far I have added the following:
Invoke the UseAuthentication method in the Configure method of the Startup.cs file:
app.UseAuthentication();
Invoke the AddAuthentication and AddCookie methods in the ConfigureServices method of the Startup.cs file:
services.AddAuthentication("MyCookieAuthenticationScheme")
.AddCookie(options => {
options.AccessDeniedPath = "/Account/Forbidden/";
options.LoginPath = "/Account/Unauthorized/";
});
In my login code I then have
await HttpContext.SignInAsync("MyCookieAuthenticationScheme", principal);
principle
is a ClaimsPrincipal
.
When I login to my site and call the line above I get the error:
InvalidOperationException: No IAuthenticationSignInHandler is configured to handle sign in for the scheme: MyCookieAuthenticationScheme
What have I missed out?
You said you wanted the default scheme to be "MyCookieAuthenticationScheme" (that's the first argument to AddAuthentication
) but you didn't add an authentication handler with that name. When you called AddCookies
, you added the handler with the scheme "Cookies" (that's the default).
You need to change your code to:
services.AddAuthentication("MyCookieAuthenticationScheme")
.AddCookie("MyCookieAuthenticationScheme", options =>
{
options.AccessDeniedPath = "/Account/Forbidden/";
options.LoginPath = "/Account/Unauthorized/";
});
See this article to better understand the primitives:
https://digitalmccullough.com/posts/aspnetcore-auth-system-demystified.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With