Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I am using MVC4, I used following approach to redirect a custom html screen upon authorization breach.</p> <p>Extend <code>AuthorizeAttribute</code> say <code>CutomAuthorizer</code> override the <code>OnAuthorization</code> and <code>HandleUnauthorizedRequest</code></p> <p>Register the <code>CustomAuthorizer</code> in the <code>RegisterGlobalFilters</code>.</p> <pre><code>public static void RegisterGlobalFilters(GlobalFilterCollection filters) { filters.Add(new CustomAuthorizer()); } </code></pre> <p>upon identifying the <code>unAuthorized</code> access call <code>HandleUnauthorizedRequest</code>and redirect to the concerned controller action as shown below.</p> <hr> <pre><code>public class CustomAuthorizer : AuthorizeAttribute { public override void OnAuthorization(AuthorizationContext filterContext) { bool isAuthorized = IsAuthorized(filterContext); // check authorization base.OnAuthorization(filterContext); if (!isAuthorized &amp;&amp; !filterContext.ActionDescriptor.ActionName.Equals("Unauthorized", StringComparison.InvariantCultureIgnoreCase) &amp;&amp; !filterContext.ActionDescriptor.ControllerDescriptor.ControllerName.Equals("LogOn", StringComparison.InvariantCultureIgnoreCase)) { HandleUnauthorizedRequest(filterContext); } } protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext) { filterContext.Result = new RedirectToRouteResult( new RouteValueDictionary{{ "controller", "LogOn" }, { "action", "Unauthorized" } }); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload