mvc Adding to the AUTHORIZE attribute

How do I create a custom attribute to extend existing Authorize attribute in MVC?

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

Derive your class from AuthorizeAttribute. Override the OnAuthorization method. Add and set up a CacheValidationHandler.

public void CacheValidationHandler( HttpContext context,
                                    object data,
                                    ref HttpValidationStatus validationStatus )
    validationStatus = OnCacheAuthorization( new HttpContextWrapper( context ) );

public override void OnAuthorization( AuthorizationContext filterContext )
    if (filterContext == null)
        throw new ArgumentNullException( "filterContext" );

    if (AuthorizeCore( filterContext.HttpContext ))
       ... your custom code ...
       SetCachePolicy( filterContext );
    else if (!filterContext.HttpContext.User.Identity.IsAuthenticated)
        // auth failed, redirect to login page
        filterContext.Result = new HttpUnauthorizedResult();
       ... handle a different case than not authenticated

protected void SetCachePolicy( AuthorizationContext filterContext )
     // ** IMPORTANT **
     // Since we're performing authorization at the action level, the authorization code runs
     // after the output caching module. In the worst case this could allow an authorized user
     // to cause the page to be cached, then an unauthorized user would later be served the
     // cached page. We work around this by telling proxies not to cache the sensitive page,
     // then we hook our custom authorization code into the caching mechanism so that we have
     // the final say on whether a page should be served from the cache.
     HttpCachePolicyBase cachePolicy = filterContext.HttpContext.Response.Cache;
     cachePolicy.SetProxyMaxAge( new TimeSpan( 0 ) );
     cachePolicy.AddValidationCallback( CacheValidationHandler, null /* data */);

Solution 2

You do not need to extend this attribute, web.config is enough. Please read about forms Element for authentication. Pay your attention on defaultUrl. This is something what you need.

  <authentication mode="Forms">
    <forms defaultUrl="YourUrlGoesHere"/>

Solution 3

public class CoolAuthorizeAttribute :  AuthorizeAttribute

Solution 4

I suggest if you just want to extend the current AuthorizeAttribute and add your own authorization on top of that, instead of overriding OnAuthorization just override AuthorizeCore and add your MyCustomAuthorizationHolds condition to it.

public class CustomAuthorizeAttribute : AuthorizeAttribute
    // This method must be thread-safe since it is called by the thread-safe OnCacheAuthorization() method.
    protected override bool AuthorizeCore(HttpContextBase httpContext)
        if (base.AuthorizeCore(httpContext) && MyCustomAuthorizationHolds)
            return true;

        return false;

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply