使用 Asp.Net 身份时如何在重定向到登录视图之前给出通知消息?

How to give a notice message before redirect to login view when use Asp.Net Identity?

我用过Asp.Net Identity framework 在我的app.There是需要,当session过期时给出提示信息,然后跳转到登录页面而不是直接跳转到登录page.Prompt 使用自定义样式的信息。 因为我的应用程序的左侧菜单使用 ajax 加载视图,所以我将 AuthorizeAttribute.HandleUnauthorizedRequest 方法重写为 return 一个 json.Now 当用户单击左侧菜单时,它可以工作 properly.But 如果用户点击F5刷新页面,页面仍然会直接跳转到登录页面。

我已经覆盖了AuthorizeAttribute.HandleUnauthorizedRequest

    protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
    {
        var httpContext = filterContext.HttpContext;
        string sintab = httpContext.Request["inTab"];            

        if (!string.IsNullOrEmpty(sintab) && bool.Parse(sintab))
        {
            var result = new JsonResult();
            result.Data = new
            {
                Authorize = false,
                Url = LOGIN_URL
            };
            result.JsonRequestBehavior = JsonRequestBehavior.AllowGet;

            filterContext.Result =result;
            return;
        }
        if (filterContext.Controller.GetType() != typeof(Controllers.HomeController) && 
            !filterContext.ActionDescriptor.ActionName.Equals("Index", StringComparison.OrdinalIgnoreCase))
        {
            string returnUrl = "/" + filterContext.Controller.GetType().Name.Replace("Controller","") + "/Index" ;
            returnUrl = httpContext.Server.UrlEncode(returnUrl);
            httpContext.Response.Redirect("~/Account/Login?ReturnUrl="+returnUrl);
            return;
        }

        base.HandleUnauthorizedRequest(filterContext);
    }

左侧菜单的loadView js代码

        $.get(url, null, function (html) {
        html = html.replace(/#%/g, "\"").replace(/%#/g, "\"");
        var json;
        try {
            json = eval("(" + html + ")");
        } catch (e) {

        }
        if (json && !json.Authorize) {
            // give an message
            layer.alert("Session timeout, please re login.", function (index) {
                window.location.href = json.Url + "?returnurl=" + encodeURI(hash);
            });
        }
        else {
            $("#content").empty().html(html);
            _initModalButton();
            $("#content").show();
        }

    }, 'html');

页面看起来像这张图片

我想知道是否有更好的方法来做到这一点,因为有很多其他按钮需要在跳转到登录页面之前检查授权状态和显示消息,以及如何在用户刷新时给出消息页面?

非常感谢!

我认为您要查找的是全球 Ajax 事件。 请检查 this,我认为这会让您的工作更轻松。