ASP.net MVC 全局授权过滤器在 AllowAnonymous 动作上强制登录
ASP.net MVC 全局授权过滤器在 AllowAnonymous 动作上强制登录
设置(使用MVC 4)
public class MyAuthorizeAttribute : AuthorizeAttribute { protected override bool AuthorizeCore(HttpContextBase httpContext) { var isAuthorised = base.AuthorizeCore(httpContext); if(isAuthorised) { // 从cookie中检索身份验证票证,并创建自定义主体并附加到httpContext.User } return isAuthorised; } }
Gloabl.asax.cs:
public static void RegisterGlobalFilters(GlobalFilterCollection filters) { filters.Add(new HandleErrorAttribute()); filters.Add(new MyAuthorizeAttribute()); }
HomeController.cs:
using System.Web.Mvc; public class HomeController : Controller { [AllowAnonymous] public ActionResult Index() { return View(); } }
问题
访问主页时会强制加载登录页面。
问题
当HomeController.Index()操作使用[AllowAnonymous]修饰时,为什么ASP会重定向我到登录视图?
我参考了这篇文章。