How to redirect the user in the wrong role in Not allowed?

When using SimpleMembershipProvider in an MVC 4 project, when a user without an appropriate role runs an action, he is redirected to Account / Login. How can I redirect the user to my own action "Insufficient permissions to view the page"?

+4
source share
1 answer

You can overwrite a method to do this.

public class CustomAuthorizeAttribute : AuthorizeAttribute { public override void OnAuthorization(AuthorizationContext filterContext) { base.OnAuthorization(filterContext); if (filterContext == null) { throw new ArgumentNullException("filterContext"); } if ({your code detecting no user is logged}) { filterContext.Result = new RedirectResult(System.Web.Security.FormsAuthentication.LoginUrl + "?returnUrl=" + filterContext.HttpContext.Server.UrlEncode(filterContext.HttpContext.Request.RawUrl)); return; } if ({your code detecting that the user has no access}) { var ViewData = new ViewDataDictionary(); ViewData.Add("Title", "No access"); ViewData.Add("Description", "blah blah blah blah blah blah blah "); filterContext.Result = new ViewResult { ViewName = "~/Views/Shared/NoAccess.cshtml", ViewData = ViewData }; } } 
+6
source

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


All Articles