进行 AJAX 调用时 ActionFilterAttribute 出现错误
我正在使用 ActionFilter 来检查用户的会话是否是该用户的最新会话。如果他们已使用另一个会话登录,则旧会话无效。因此,我会检查调用的每个操作,如果它们没有使用最新的会话,则将其注销。我遇到的问题是我的方法不适用于 AJAX 调用。正常的 GET 或 POST 调用工作正常。
public class DuplicateLoginFilterAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
var user = Membership.GetUser(HttpContext.Current.User.Identity.Name);
if (user != null)
{
if (!PortalDA.GetUserSession(user.UserName).Trim().Equals(HttpContext.Current.Session.SessionID))
{
FormsAuthentication.SignOut();
filterContext.Result = new RedirectResult(@"\Home\SessionExprired");
}
}
base.OnActionExecuting(filterContext);
}
我想解决这个问题的最大问题是我收到错误,但我无法在任何地方捕获它。
我在 webconfig 中打开了自定义错误。我正在重写所有控制器中的 OnException 事件。我还重写了 global.asax 中的 Application_Error 事件。
我已经单步执行了代码,当我进行 AJAX 调用时,我返回 SessionExpired 视图。但在那之后的某个地方,错误发生了,我无法再进一步了。被覆盖的错误事件中永远不会捕获该错误。我被发送到自定义错误页面。
我怀疑它与 HTTPHeaders 有关,但我对它们了解不够,不知道如果是 AJAX 调用如何修复它。
谢谢!
I am using a ActionFilter to check if a User's session is the most current session for that user. If they have signed in with another session then the old session is invalid. So, I am checking each action as it is called and signing them out if they are not using the most current session. The problem I am having is that my approach does not work with AJAX calls. The normal GET or POST calls work fine.
public class DuplicateLoginFilterAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
var user = Membership.GetUser(HttpContext.Current.User.Identity.Name);
if (user != null)
{
if (!PortalDA.GetUserSession(user.UserName).Trim().Equals(HttpContext.Current.Session.SessionID))
{
FormsAuthentication.SignOut();
filterContext.Result = new RedirectResult(@"\Home\SessionExprired");
}
}
base.OnActionExecuting(filterContext);
}
I guess my biggest problem to solving this is that I get an error but I can't catch it any where.
I have custom Errors turned On in the webconfig. I am overriding the OnException event in all my controllers. I am also overriding the Application_Error event in the global.asax.
I have stepped through the code, when I make an AJAX call, I return the SessionExpired View. But somewhere after that the error occurs and I can't step any further. The error is never caught in the error events that were overridden. And I get sent to the custom Error page.
I suspect it has something to do with the HTTPHeaders but I don't understand enough about them to know how to fix it if it is AJAX call.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用您的路由进行重定向,而不是对 URL 进行硬编码:
然后确保
HomeController
上存在SessionExpired
操作以及相应的视图,并且该操作已被命中。显然,由于您是在 AJAX 调用内部进行重定向,因此您在 AJAX 调用的成功回调中得到的只是作为参数传递的 SessionExpired 视图的 HTML。不要指望浏览器会重定向。您必须使用 window.location.href 手动重定向浏览器。这就是为什么在这种情况下(AJAX 情况)最好返回 Json 结果而不是重定向到会话过期操作。然后在客户端您可以手动执行重定向。Try redirecting using your routes and not hardcoding the url:
Then make sure that the
SessionExpired
action exists onHomeController
along with the corresponding view and that this action is hit. Obviously since you are redirecting inside an AJAX call, all that you will get in the success callback of your AJAX call is the HTML of this SessionExpired view passed as argument. Don't expect the browser to redirect. You will have to use window.location.href to manually redirect the browser. That's the reason why in this case (the AJAX one) it is better to return a Json result instead of redirecting to a session expired action. Then on the client side you could perform the redirect manually.