Redirecting MVC3 to the "Unauthorized" page does not work?

After you ask this question about authorization, I added a new custom attribute to redirect unauthorized users to a page that has more detailed information about the access request, etc. etc.

public class RedirectAuthorize:AuthorizeAttribute { protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext) { //base.HandleUnauthorizedRequest(filterContext); filterContext.Result = new RedirectResult("Unauthorized"); } } 

I decorated my home controller with this attribute and the correct one (Role = "..."), and it "works." Those. he refers to the method as and when expected.

I added a vanilla look to the shared views folder named Unauthorized.cshtml , but I just got the message "Could not find resource." Error 404.

Is the display URL displayed correctly?

My guess: do I need to specify a controller / action instead of a page? but Error.cshtml error handler redirect to Error.cshtml without the need for a controller?

Thanks for any help.

+4
source share
1 answer

The problem is that you are using RedirectResult , which causes the browser to request this URL. But when the browser requests it, there is no corresponding route. Therefore, it throws an error 404.

Try

 filterContext.Result = new ViewResult { ViewName = "Unauthorized" }; 
+6
source

Source: https://habr.com/ru/post/1391379/


All Articles