Skip to content

Instantly share code, notes, and snippets.

@csharpfritz
Created March 24, 2011 00:04
Show Gist options
  • Save csharpfritz/884305 to your computer and use it in GitHub Desktop.
Save csharpfritz/884305 to your computer and use it in GitHub Desktop.
A half-baked authorize attribute example
public class JeffAuthorizeAttribute : ActionFilterAttribute
{
#region Fields
private Permissions _Required;
#endregion
public JeffAuthorizeAttribute(Permissions requiredPerm)
{
_Required = requiredPerm;
}
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
var baseController = filterContext.Controller as BaseController;
var notAuthorizedResult = new RedirectToRouteResult(new RouteValueDictionary() { { "controller", "Home" }, { "action", "NotAuthorized" } });
int pVal = (int) _Required;
var perm = PermissionList.Available.First(p => p.Id == pVal);
if (baseController == null || baseController.JeffUser == null)
{
filterContext.Result = notAuthorizedResult;
}
else if (!baseController.JeffUser.IsAllowed(perm))
{
filterContext.Result = notAuthorizedResult;
}
else
{
base.OnActionExecuting(filterContext);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment