我有一个名为ForgetPassword的动作.每次匿名尝试检索操作时,他/她都会被重定向到登录页面.以下是我的实现.
- public ActionResult ForgotPassword(string UserName)
- {
- //More over when i place a breakpoint for the below line
- //its not even getting here
- return View("Login");
- }
这是我的web.config文件的一部分
- <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="Images">
- <system.web>
- <authorization>
- <allow users="*"/>
- </authorization>
- </system.web>
- </location>
- <authentication mode="Forms">
- <forms loginUrl="/Home/Login" timeout="5" slidingExpiration="false" />
- </authentication>
解决方法
因为你通过使用来拒绝所有人的申请.
- <authorization>
- <deny users="?"/>
- </authorization>
恕我直言,您不应该使用web.config来控制应用程序的身份验证,而是使用Authorize属性.
在RegisterGlobalFilters方法下的Global.asax文件中添加它
- public static void RegisterGlobalFilters(GlobalFilterCollection filters)
- {
- filters.Add(new HandleErrorAttribute());
- filters.Add(new AuthorizeAttribute()); //Added
- }
或者您也可以使用[授权]装饰您的控制器
- [Authorize]
- public class HomeController : Controller
- {
- ...
- }
如果您使用的是ASP.NET MVC4,则对于需要匿名访问的操作,请使用AllowAnonymous属性
- [AllowAnonymous]
- public ActionResult ForgotPassword() {
- //More over when i place a breakpoint for the below line
- //its not even getting here
- return View("Login");;
- }
根据Reference,您不能使用路由或web.config文件来保护您的MVC应用程序.保护MVC应用程序唯一受支持的方法是将Authorize属性应用于每个控制器,并在登录和注册操作上使用新的AllowAnonymous属性.根据当前区域做出安全决策是非常糟糕的事情,并会将您的应用程序打开到漏洞中.