Skip to content

Instantly share code, notes, and snippets.

@yannduran
Forked from bjcull/AuthorizeRedirect.cs
Created September 27, 2015 13:33
Show Gist options
  • Save yannduran/780f16df9376272a2526 to your computer and use it in GitHub Desktop.
Save yannduran/780f16df9376272a2526 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