[Authorize] Attribute is a nice and convenient invention of MS, and I hope that he can solve the problems that I have now.
More specific:
When the current client is not authenticated - [Authorize] redirects from the protected action to the login page and after successful login - returns the user, this is good.
But when the current client is already authenticated but not authorized to start a specific action - all I need to do is just show my 403 shared page.
Is this possible without moving the authorization logic into the body of the controller?
Update : The behavior I need should be semantically equal to this sketch:
public ActionResult DoWork() { if (!NotAuthorized()) { // this should be not redirect, but forwarding return RedirectToAction("403"); } return View(); }
so - there should be no redirects and URLs that should remain unchanged, but the page content should be replaced with 403 pages
Update 2 . I implemented the sketch as follows:
[HandleError] public class HomeController : Controller { public ActionResult Index() { ViewData["Message"] = "Welcome to ASP.NET MVC!"; return View(); } [CustomActionFilter] public ActionResult About() { return View(); } public ActionResult Error_403() { return Content("403"); } } public class CustomActionFilter : ActionFilterAttribute { public override void OnActionExecuting(ActionExecutingContext filterContext) { filterContext.Result = new ContentResult { Content = "403" }; } }
And it doesnβt work out how to redirect execution correctly to HomeController.Action_403 () so that it displays 403.
Update 3 :
filterContext.Result = new ViewResult() { ViewName = "Error_403" };
so this is the answer to how to render a certain presentation template ... but still donβt know how to start another controller - in any case, this is a pretty good solution.
zerkms Apr 05 2018-10-14T00: 00Z
source share