My goal is to show up a modal dialog on a view when a Unathorized event is thrown. I have override the OnAuthorization method of my custom AuthorizedAttribute like this post:
ASP.NET MVC - How to show unauthorized error on login page?
But the problem is that the TempData is sent to the unauthorized controller (ClientController) and it is not the desired behaviour.
What I need is: call Home/Index/ToClientes and when it tries to get Client/Index show a Modal Dialog on Home/Index to inform the user about the unauthorized event.
[HttpPost]
public ActionResult ToClientes()
{
return RedirectToAction("Index", "Client");
}
At present the TempData is read inside ClientController I would need to read it on HomeController to show the modal popup view explaining the error.
My Overrride OnAuthorization method is similar to the one on the referenced post
public class CustomAuthorizeAttribute : AuthorizeAttribute
{
private bool _isAuthorized;
protected override bool AuthorizeCore(System.Web.HttpContextBase httpContext)
{
_isAuthorized = base.AuthorizeCore(httpContext);
return _isAuthorized;
}
public override void OnAuthorization(AuthorizationContext filterContext)
{
base.OnAuthorization(filterContext);
if(!_isAuthorized)
{
filterContext.Controller.TempData.Add("RedirectReason", "Unauthorized");
}
}
}
Is there anyway to send the TempData to the controller that called the function but not the one that generates the onAuthorization method? Or Is there any other easier way to show a modal popup when an unauthorized event is raised?