After asking this question about authorization, I’ve added a new custom attribute in an attempt to redirect unauthorised users to a page which has more details on requesting access etc etc.
public class RedirectAuthorize:AuthorizeAttribute
{
protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
{
//base.HandleUnauthorizedRequest(filterContext);
filterContext.Result = new RedirectResult("Unauthorized");
}
}
I’ve decorated my Home controller with this attribute and the correct (Role=”…”) and this “works”..i.e. it hits the method as and when expected.
I’ve added a vanilla view to the Shared views folder called Unauthorized.cshtml but I just get “The resource cannot be found” 404 error.
The URL displayed appears to be correct?
My guess is I need to specify a controller/action instead of a page? but the Error handler redirects to Error.cshtml without needing a controller?
Thanks for any assistance.
Problem is you are using
RedirectResultwhich forces the browser to request the given URL. But when the browser requests it there is no route that matches. Hence it throws a 404 error.Try