In my application I have several areas(views) which should be accessible only for these users who have certain privilages. IF currently logged user do not has right to see given view a popup window should appear. In this moment user can give some additional information in order see the view. The point is that the user can't leave current view untill he/she provide this information. So far I think I could do this that way. First of all I defined custom AuthorizeAttribute. This attribute is applied to controler which is responsible for protecting restricted views. My attribute looks like this
public class PopupAuthorizeAttribute : AuthorizeAttribute
{
public override void OnAuthorization(AuthorizationContext filterContext)
{
if (filterContext.HttpContext.Session["confirmed"] == null)
{
filterContext.Controller.ViewData["ShowPopup"] = true;
}
else
filterContext.Controller.ViewData["ShowPopup"] = false;
}
}
Next I modified _Layout.cshtml to look this way
...
...
<body>
<div id="main">
@{
if ((ViewData[ShowPopup] != null && (bool)ViewData[ShowPopup]))
{
<script type="text/javascript">
showPopUp();
</script>
}
}
<div id="header">
title
</div>
<div id="menu">
@{
Html.RenderAction("TopMenu", "Menu");
}
</div>
<div id="treeView">
@{
Html.RenderAction("TreeMenu", "Tree");
}
</div>
<div id="content">
@RenderBody()
</div>
</div>
</body>
...
...
Unfortunatelly the result was different than I expected. Now the view which supposed to be inaccessible for user is rendered and the popup is shown. I tried to redirect current view to previous one in my custom attribute doing sth like that
public class PopupAuthorizeAttribute : AuthorizeAttribute
{
public override void OnAuthorization(AuthorizationContext filterContext)
{
if (filterContext.HttpContext.Session["confirmed"] == null)
{
filterContext.Controller.ViewData["ShowPopup"] = true;
filterContext.Result =
new RedirectResult(filterContext.RequestContext.HttpContext.Request.UrlReferrer.ToString());
}
else
filterContext.Controller.ViewData["ShowPopup"] = false;
}
}
However if I do that I losse information stored in ViewData. Is there any elegant or better way to achieve this functionality. Unfortunatelly I can't redirect a user to "normal" page, this must be done in popupwindow.