我们使用 mvc 创建了一个网站,其中我有 3 个控制器:
AccountController
HomeController
ContactController
我们在 ActionFilters 中编写了自定义授权,我们在其中检查会话是否已过期。如果会话过期,我们将重定向到登录页面。这是 ActionFilter 中的代码:
public override void OnAuthorization(AuthorizationContext filterContext)
{
var user = filterContext.HttpContext.User;
var UserProcesses = HttpContext.Current.Session["UserProcesses"] != null ? ((List<string>)HttpContext.Current.Session["UserProcesses"]) : null;
var currentRequest = filterContext.ActionDescriptor.ActionName.ToLower();
var currentController = filterContext.RouteData.Values["controller"].ToString();
if (!string.IsNullOrEmpty(currentController))
{
switch (currentController.ToLower())
{
case "home":
{
if (UserProcesses == null)
{
filterContext.Result = new RedirectToRouteResult(
new RouteValueDictionary(
new
{
Controller = "Account",
Action = "Login"
})
);
}
break;
case "contact":
{
if (UserProcesses == null)
{
filterContext.Result = new RedirectToRouteResult(
new RouteValueDictionary(
new
{
Controller = "Account",
Action = "Login"
})
);
}
break;
}
}
}
这非常有效,即当会话到期并且用户单击网站上的任何链接时,他将被重定向到登录页面,其地址为
http://websitename/Account/Login
但是现在我们想在页面(无论用户在哪个页面)上显示一个弹出窗口,它将显示消息“该会话已过期”,并且在关闭该弹出窗口时,用户将被重定向到登录页面。
我无法弄清楚如何从 ActionFilter 本身调用 Jquery 弹出窗口。我在网上搜索了许多解决方案,但找不到正确的解决方案。有人建议使用HandleUnauthorizedRequest来调用 Action 方法(Call RedirectToAction from ActionFilter)。但是,我很困惑是只使用HandleUnauthorizedRequest还是在我的代码中同时使用 this 和OnAuthorization 。有人可以提出解决方案吗?
注意:我有一个通用视图,我可以在其中保留 html 以供在整个网站上使用。