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

问题描述 投票:0回答:3

我正在按照 Cookie 中间件 为我的简单 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 但页面无法打开并收到错误消息:

本地主机页面无法正常工作:本地主机重定向您太多次。

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

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

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

c# authentication cookies asp.net-core-mvc asp.net-core-1.0
3个回答
9
投票

这是因为您的

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


1
投票

就我而言,

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

本地主机页面无法正常工作:本地主机重定向了太多次 次。

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


0
投票

只是为其他遇到此问题的人提供的附录 - 检查 web.config 以查看它是否有类似的内容:

<authorization>
    <deny users="?"/>
</authorization>

即使我将 Windows 身份验证设置为“禁用”并将匿名身份验证设置为“启用”,这也会导致我的 Web 表单应用程序出错

© www.soinside.com 2019 - 2024. All rights reserved.