Search code examples
c#asp.net-mvcauthenticationasp.net-coreclaims

ASP.Net Mvc Core Project unauthorized request doesn't redirect to Login page


I am implementing custom Authentication in an ASP.Net Core project without identity.

I have include CookieAuthentication just before UseMvc

app.UseCookieAuthentication(new CookieAuthenticationOptions()
{
   AuthenticationScheme = "Cookie",
   LoginPath = new PathString("/Account/Login/"),
   AccessDeniedPath = new PathString("/Account/Forbidden/"),
   AutomaticAuthenticate = true,
   AutomaticChallenge = true
});

Add Authorization for custom policy

services.AddAuthorization(options =>
{
   options.AddPolicy("CustomPolicy",
        policy => policy.RequireClaim("CustomClaim"));
 });

And at home controller I used the appropriate auth attribute

[Authorize(Policy = "CustomPolicy")]
public class HomeController : Controller { }

I would expect to be redirected to /Account/Login/ every time I try to access /Home/Index but I only get 401.

I followed these two tutorials:

What I am missing?


Update: This problem exists only when I am using IIS to run the app, from the exe the redirection is working properly.


Solution

  • I updated all nuget packages and it solved all the problems.