gpt4 book ai didi

javascript - 使用 Asp.Net Identity 时如何在重定向到登录 View 之前发出通知消息?

转载 作者:行者123 更新时间:2023-12-02 14:39:38 24 4
gpt4 key购买 nike

我的应用中使用了Asp.Net Identity框架。有一个需求,当 session 过期时给出提示信息,然后跳转到登录页面,而不是直接跳转到登录页面。提示信息使用自定义风格。因为我的应用程序的左侧菜单使用ajax加载 View ,所以我覆盖了AuthorizeAttribute.HandleUnauthorizedRequest方法以返回json。现在当用户单击左侧菜单时,它可以正常工作。但是如果用户通过单击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');

页面看起来像这个图片 enter image description here

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

非常感谢!

最佳答案

我认为您正在寻找的是全局 Ajax 事件。请检查this ,我认为这会让你的工作变得更轻松。

关于javascript - 使用 Asp.Net Identity 时如何在重定向到登录 View 之前发出通知消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37127891/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com