Asp.net unauthorized kernel migration does not occur

I am trying to use cookie validation in the main Asp.net application (dnx 4.5). Please note that since I have my own authentication mechanism (radius), I do not use the ready-made mechanism provided by basic authentication. When the user is not authenticated, I want to redirect to the login page.

I added the following code to Startup.cs. The idea should be redirected to the login controller if the user is not authenticated:

app.UseCookieAuthentication(options => { options.LoginPath = new Microsoft.AspNet.Http.PathString("/Login"); }); 

In my home controller, I have:

 [Authorize] public IActionResult Index() { return View(); } 

In my entry controller, I return a view corresponding to the radius entry form:

 [AllowAnonymous] public IActionResult Index() { return View(); } 

However, when I launch the application, redirection never occurs. Looking at the console output, I see the following:

 warn: Microsoft.AspNet.Mvc.Controllers.ControllerActionInvoker[0] Authorization failed for the request at filter 'Microsoft.AspNet.Mvc.Filters.AuthorizeFilter'. info: Microsoft.AspNet.Mvc.ChallengeResult[1] Executing ChallengeResult with authentication schemes (). info: Microsoft.AspNet.Mvc.Infrastructure.MvcRouteHandler[2] Executed action ThingsProjectorWeb.Controllers.HomeController.Index in 0ms info: Microsoft.AspNet.Hosting.Internal.HostingEngine[2] Request finished in 0.0016ms 401 

Any help on how to set this up correctly would be greatly appreciated. Thanks!

+5
source share
1 answer

OK figured it out. Everything is explained here: https://docs.asp.net/en/latest/security/authentication/cookie.html

I was missing options.AutomaticChallenge = true; which automatically redirects you to the login controller.

Here are the updated options:

 app.UseCookieAuthentication(options => { options.LoginPath = new Microsoft.AspNet.Http.PathString("/Login"); options.AutomaticChallenge = true; }); 

UPDATE:

Starting with version 1.1.0 this is:

 app.UseCookieAuthentication(new CookieAuthenticationOptions { LoginPath = new Microsoft.AspNetCore.Http.PathString("/Account/Login"), AutomaticChallenge = true }); 
+8
source

All Articles