3

我们使用 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 以供在整个网站上使用。

4

2 回答 2

2

我正在考虑使用变量的解决方案,并且根据该变量,您会显示或不显示弹出窗口。我懒得实际编写代码,但我找到了一个描述解决方案的链接:

ASP.NET MVC 授权属性启动模式?

于 2015-09-18T12:56:37.570 回答
1

还有另一种解决方法,即只需转到_Layout.cshtml文件并输入以下代码:

<script type="text/javascript">
    jQuery(document).ready(function () {          

        var SessionValue = '@Request.RequestContext.HttpContext.Session["YourSessionKey"]';


        if (SessionValue == "") {
            alert("your session is expired please try to Re-login");
            javascript: document.getElementById('logoutForm').submit();
        }
    });
</script>

logoutForm的注销表单的 id 在哪里_Layout.cshtml

于 2016-07-14T07:24:51.657 回答