I have an MVC website in which access is based on different roles. As soon as the user logs in, they can see the navigation to the pages for which they are authorized. However, some users may still try to access the pages using the direct URL. If so, the system automatically redirects them to the login page. Instead of the login page, I want to redirect them to another view (unauthorized).
Web.Config has the following entry:
<customErrors mode="On"> <error statusCode="401" redirect="~/Home/Unauthorized" /> <error statusCode="404" redirect="~/Home/PageNotFound" /> </customErrors> <authentication mode="Forms"> <forms name="Development" loginUrl="~/Account/Login" cookieless="UseCookies" timeout="120"></forms> </authentication>
I also registered these routes at Global.asax.cs.
routes.MapRoute( name: "Unauthorized", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Unauthorized", id = UrlParameter.Optional } ); routes.MapRoute( name: "PageNotFound", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "PageNotFound", id = UrlParameter.Optional } );
Would that be enough?
authorization asp.net-mvc unauthorized
user2739418
source share