How does the Authorize tag work? – ASP.NET MVC

The Authorize tag uses all the built in membership checks from ASP.NET. It’s VERY easy to roll your own tag. For example:

public class MyAuthorize : AuthorizeAttribute
{
    protected override bool AuthorizeCore(HttpContextBase httpContext)
    {
        if (httpContext == null) throw new ArgumentNullException("httpContext");

        // Make sure the user is authenticated.
        if (httpContext.User.Identity.IsAuthenticated == false) return false;

        // Do you own custom stuff here
        bool allow = CheckIfAllowedToAccessStuff();

        return allow;
    }
}

You then can use the [MyAuthorize] tag which will use your custom checks.

Leave a Comment