gpt4 book ai didi

asp.net-mvc-3 - 此页面未正确重定向 MVC3 + AJAX + 表单例份验证错误

转载 作者:行者123 更新时间:2023-12-01 03:53:47 24 4
gpt4 key购买 nike

编辑:删除了不相关的代码/描述,因为问题不仅仅与那里的初始代码有关。

我有一个基于 MVC3 的应用程序,它使用了大量的 Ajax 调用(例如使用 jqGrid)和表单例份验证。在大多数情况下,我还在使用 Ajax 调用的某些 Controller /操作上使用 [Authorize] 属性。每隔一段时间,我的应用程序就会因“此页面未正确重定向”或“此页面有重定向循环”而失败。

我用 fiddler 检查了这个。以用户身份登录并尝试访问需要身份验证的页面后,有时我会被重定向到帐户/登录,然后进入无限循环。当我通过 Ajax 调用调用具有 Authorize 属性的任何 Controller /操作时,通常会发生这种情况。该应用程序似乎向帐户/登录发出 302 重定向。然后帐户/登录调用似乎重定向到自身。 Fiddler 上的 textView 显示以下内容。

<html><head><title>Object moved</title></head><body>
<h2>Object moved to <a href="/Account/LogOn">here</a>.</h2>
</body></html>

我的 Global.asax 文件中有以下内容
protected void Application_EndRequest()
{
var context = new HttpContextWrapper(this.Context);
//if we are an ajax request and forms authentication caused a 302, then we actually need to do a 401
if (System.Web.Security.FormsAuthentication.IsEnabled && context.Response.StatusCode == 302 && context.Request.IsAjaxRequest())
{
context.Response.Clear();
context.Response.StatusCode = 401;
}
}

这在我的主布局页面中
<script type="text/javascript">
$(document).ajaxError(function (xhr, props) {
if (props.status == 401) {
location.reload();
}
});

</script>

我的表单例份验证的 web.config 设置有这个
<authentication mode="Forms">
<forms loginUrl="~/Account/LogOn" timeout="2880"/>
</authentication>

重定向每隔一段时间就会开始,并且它也不是使用相同的 Controller /操作。只是看起来很随意。我不确定用户的 cookie 是否已过期并由于某种原因导致此问题,或者是否是应用程序池回收的问题。任何有关如何解决此问题的建议将不胜感激。最近几天一直在努力解决这个问题,所以专家的任何帮助都会很棒。谢谢你

最佳答案

装饰你的操作方法授权 属性使其可用于经过身份验证/登录的用户,然后检查用户角色。

关于asp.net-mvc-3 - 此页面未正确重定向 MVC3 + AJAX + 表单例份验证错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19212004/

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