Created
April 29, 2011 19:07
-
-
Save briandonahue/948822 to your computer and use it in GitHub Desktop.
Simple way to override controller auth at the action level
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// New attribute that is only usable at the method (action) level | |
[AttributeUsage(AttributeTargets.Method, Inherited = true, AllowMultiple = true)] | |
public class ActionAuthorizeAttribute : AuthorizeAttribute { | |
} | |
// New attribute that is only usable at the class (controller) level | |
[AttributeUsage(AttributeTargets.Class, Inherited = true, AllowMultiple = true)] | |
public class ControllerAuthorizeAttribute : AuthorizeAttribute | |
{ | |
public override void OnAuthorization(AuthorizationContext filterContext) | |
{ | |
//If the target action has been decorated with an ActionAuthorizeAttribute, skip controller auth | |
if (filterContext.ActionDescriptor.GetCustomAttributes(true).Any(a => a is ActionAuthorizeAttribute)) | |
{ | |
return; | |
} | |
base.OnAuthorization(filterContext); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment