Asp.net 核心 cookie 身份验证请求过多

Asp.net core cookie authentication too many requests

我正在关注 Cookie Middleware 为我的简单 Web 应用程序添加身份验证。

我在 startup.cs 配置方法中添加了以下代码。

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

当我访问 /Home 时,它​​重定向到 /Account/Login 但页面打不开并收到错误消息

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

url多次附加相同的登录页面,看起来像

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

我做错了什么?帮我解决这个问题。

这是因为您的 /Account/Login 操作还需要登录,这会导致无限重定向循环。在此操作上添加 AllowAnonymousAttribute

在我的例子中,/Account/Login 控制器已经具有 AllowAnonymous 属性,但我不断收到消息

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

问题出在 IIS 配置中。我不得不更改身份验证选项卡中的一些选项。我必须禁用 Windows 身份验证选项并启用匿名身份验证选项。