我已经在我的Asp.Net标识框架中使用了app.There是一种需要,当会话到期时发出提示消息,然后跳到登录页面,而不是使用自定义样式直接跳转到登录page.Prompt信息。因为我的应用程序的左边菜单用ajax加载视图,所以当用户单击左菜单时,我使用AuthorizeAttribute.HandleUnauthorizedRequest方法返回json.Now,如果用户通过单击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');这个页面看起来像这张图片

我想知道是否有更好的方法来做到这一点,因为还有很多其他按钮需要在跳到登录页面之前检查授权状态和显示消息,以及当用户刷新页面时如何给出消息?
非常感谢!
发布于 2016-05-10 06:09:53
我认为你正在寻找的是全球Ajax事件。请查查这,我认为这能让你的工作更轻松。
https://stackoverflow.com/questions/37127891
复制相似问题