Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

InvalidOperationException: No IAuthenticationSignInHandler is configured to handle sign in for the scheme: MyCookieAuthenticationScheme

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?

like image 936
Liam Avatar asked Aug 19 '17 21:08

Liam


1 Answers

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

like image 178
davidfowl Avatar answered Oct 14 '22 22:10

davidfowl