我有以下问题:当我的会话到期并且用户单击 Ajax.Actionlink 时,登录页面返回为部分页面
问问题
250 次
2 回答
2
创建一个实现 IAuthorizationFilter 的 ActionFilterAttribute
并在 OnAuthorization 检查用户是否已注销,
如果他已注销并且这是一个 Ajax 请求(filterContext.HttpContext.Request.IsAjaxRequest()) - 将 filterContext.Result 设置为不同的结果。我返回一个带有 HttpStatusCode.Unauthorized 的结果,这样我就可以在客户端连接它并做出相应的反应
从未使用过 Ajax.Actionlink,我对所有 Ajax 调用都使用 jQuery,并且有一个全局挂钩来捕获带有错误状态代码的结果
于 2012-04-30T08:52:20.987 回答
1
您将在您的操作方法的顶部需要这样的东西
if (Session["UserID"] == null && Request.IsAjaxRequest())
{
return Content("<div class=\"error\">Your session was expired. Press Logout and then login again to continue</div>");
}
更新 (您需要在检查授权时指定它,无论您创建了什么机制)
public ActionResult Index()
{
try
{
List<string> rights = objAuthorization.CheckMemberRightsOnPage("page1");
if (!rights.Contains("View"))
{
if (Session["UserID"] == null && Request.IsAjaxRequest())
{
return Content("<div class=\"error\">Your session was expired. Press Logout and then login again to continue</div>");
}
return RedirectToAction("Restricted", "UserLogin", new { url = HttpUtility.HtmlEncode(Request.Url.AbsolutePath.Substring(1)) });
}
ViewData["objAuthorization1"] = rights;
// your other things here
}
catch(Exception ex){
ViewData["ErrorMessage"] = "An error occurred: " + ex.Message;
return View(); }
}
于 2012-04-30T08:49:08.417 回答