允许匿名访问忘记密码页面

Allow Anonymous Access to ForgotPassword Page

我的登录页面上有一个 link 到 /ForgotPassword。单击它时,我将被重定向到 /Login?ReturnUrl=%2fForgotPassword。这是一个使用基于表单的身份验证的 MVC5 应用程序。我正在尝试使用 AllowAnonymous,但它不起作用。 网络配置:

  <location path=".">
  <system.web>
  <authorization>
    <deny users="?"/>
  </authorization>
  </system.web>
  </location>
  <location path="Content">
  <system.web>
  <authorization>
    <allow users="*"/>
  </authorization>
  </system.web>
  </location>
  <location path="Scripts">
  <system.web>
  <authorization>
    <allow users="*"/>
  </authorization>
  </system.web>
  </location>
  <location path="App_Themes">
  <system.web>
  <authorization>
    <allow users="*"/>
  </authorization>
  </system.web>
 </location>

<authentication mode="Forms">
  <forms loginUrl="~/Login" timeout="15"/>
</authentication>

忘记密码控制器:

 [AllowAnonymous]
 public class ForgotPasswordController : Controller
 {
    [AllowAnonymous]
    public ActionResult Index()
    {          
        return View();
    }
 }

没有这样的运气。除了 _ViewStart,我找不到正在使用的任何应该用 AllowAnonymous 装饰的东西。

我在这里缺少什么?谢谢!

不要在 we.config 中使用授权标签,只在控制器和操作中使用授权属性。

web.config 中的授权标签对于 allow/restrict 访问文件很有用,并且适用于 Web 表单和静态内容。

但是在MVC中有映射到控制器和动作的路由,你可能在同一个控制器中有不同的动作和不同的授权标签,例如:你可以对整个控制器使用[Authorize],并且只允许通过仅对所需操作使用 [AllowAnonymous] 属性,为匿名用户执行一项操作。

在此处阅读更多内容:https://weblogs.asp.net/jongalloway/asp-net-mvc-authentication-global-authentication-and-allow-anonymous