Search code examples
c#authenticationcookiesasp.net-core-mvcasp.net-core-1.0

Asp.net core cookie authentication too many requests


I am following Cookie Middleware to add authentication for my simple web application.

I have added the below code in startup.cs Configure method.

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

When i accessing /Home, it redirects to /Account/Login but page doesn't open and getting error message as

The localhost page isn’t working : localhost redirected you too many times.

The url appends with same login page many times and look like

http://localhost:5000/Account/Login/?ReturnUrl=%2FAccount%2FLogin%2F%3FReturnUrl%3D%252FAccount%252FLogin%252F%253FReturnUrl%253D%25252FAccount........%2FHome

What i did wrong? Help me to solve this.


Solution

  • This is because your /Account/Login action also requires login, which causes an infinite redirect loop. Add AllowAnonymousAttribute on this action.