[CustomAuthorize] //这是我自定义的AuthorizeAttribute
public class AccountController : BaseController
{
//
// GET: /Account/Login
[AllowAnonymous]
public ActionResult Login(string returnUrl)
{
ViewBag.ReturnUrl = returnUrl;
return View();
}
}
在加入自定义的AuthorizeAttribute时,第一次测没有经过CustomAuthorize。在我重新生成后,对于Login()上面添加[AllowAnonymous]并不能让[AllowAnonymous]起作用。于是我重新来了两次,确定了[AllowAnonymous]对于我自定义的AuthorizeAttribute的确失效了。而我的[CustomAuthorize]是针对整个AccountController
public class CustomAuthorizeAttribute : AuthorizeAttribute
{
public override void OnAuthorization(AuthorizationContext filterContext)
{
//关于判断AllowAnonymousAttribute
bool flag = ((ReflectedActionDescriptor)filterContext.ActionDescriptor).MethodInfo.IsDefined(typeof(AllowAnonymousAttribute), true) ||
filterContext.ActionDescriptor.ControllerDescriptor.ControllerType.IsDefined(typeof(AllowAnonymousAttribute), true);
if (flag)
{
return;
}
}
}
在我找了很多方法后,发现添加了这段代码后对于我的[CustomAuthorize],通过添加[AllowAnonymous]能够跳过验证,并对我的Controller其他方法没有影响。