首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当使用Asp.Net标识时,如何在重定向登录视图之前发出通知消息?

当使用Asp.Net标识时,如何在重定向登录视图之前发出通知消息?
EN

Stack Overflow用户
提问于 2016-05-10 01:29:46
回答 1查看 156关注 0票数 0

我已经在我的Asp.Net标识框架中使用了app.There是一种需要,当会话到期时发出提示消息,然后跳到登录页面,而不是使用自定义样式直接跳转到登录page.Prompt信息。因为我的应用程序的左边菜单用ajax加载视图,所以当用户单击左菜单时,我使用AuthorizeAttribute.HandleUnauthorizedRequest方法返回json.Now,如果用户通过单击F5刷新页面,页面仍然会直接跳转到登录页面。

我已经超越了AuthorizeAttribute.HandleUnauthorizedRequest

代码语言:javascript
复制
    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代码

代码语言:javascript
复制
        $.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');

这个页面看起来像这张图片

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

非常感谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-05-10 06:09:53

我认为你正在寻找的是全球Ajax事件。请查查,我认为这能让你的工作更轻松。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37127891

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档