设置(使用MVC 4)
public class MyAuthorizeAttribute : AuthorizeAttribute {
protected override bool AuthorizeCore(HttpContextBase httpContext) {
var isAuthorised = base.AuthorizeCore(httpContext);
if(isAuthorised) {
// retrieve authentication ticket from cookie and
// create custome principal and attach to
// httpContext.User
}
return isAuthorised;
}
}
Run Code Online (Sandbox Code Playgroud)
Gloabl.asax.cs:
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
filters.Add(new MyAuthorizeAttribute());
}
Run Code Online (Sandbox Code Playgroud)
HomeController.cs:
using System.Web.Mvc;
public class HomeController : Controller
{
[AllowAnonymous]
public ActionResult Index()
{
return View();
}
}
Run Code Online (Sandbox Code Playgroud)
问题
对主页的调用会强制加载登录页面.
题
当HomeController.Index()动作用[AllowAnonymous]修饰时,为什么ASP会将我重定向到登录视图?
我正在使用这篇文章作为参考
asp.net security authentication asp.net-mvc forms-authentication