Skip to content

Instantly share code, notes, and snippets.

@bjcull
Last active September 27, 2015 13:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bjcull/11026803 to your computer and use it in GitHub Desktop.
Save bjcull/11026803 to your computer and use it in GitHub Desktop.
Authorize attribute that redirects to view if you're logged in but don't have permission
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public class AuthorizeRedirect : AuthorizeAttribute
{
public string RedirectUrl = "~/Error/Unauthorized";
protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
{
base.HandleUnauthorizedRequest(filterContext);
if (filterContext.RequestContext.HttpContext.User.Identity.IsAuthenticated)
{
filterContext.RequestContext.HttpContext.Response.Redirect(RedirectUrl);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment