Skip to content

Instantly share code, notes, and snippets.

@briandonahue
Created April 29, 2011 19:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save briandonahue/948822 to your computer and use it in GitHub Desktop.
Save briandonahue/948822 to your computer and use it in GitHub Desktop.
Simple way to override controller auth at the action level
// 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