c# - MVC4 : After server restart, 无法发布 ajax 内部服务器错误 500

标签 c# ajax asp.net-mvc asp.net-mvc-4 cookies

我正在构建一个 MVC 应用程序。它的行为很奇怪。如果我在我的开发服务器上运行它(visual studio 调试),即使我通过更改 web.config 多次重新启动我的应用程序,它也能正常运行,但是当我托管它时,它的行为很奇怪。

第一次加载,一切正常。我可以正常加载所有页面,通过 ajax 发布等,但是在我使用 HttpRuntime.UnloadAppDomain(); 重新启动服务器后然后每次我通过 ajax 发布时,我总是得到

internal server error 500

我不知道应用程序发生了什么,但我怀疑客户端(用户)缓存/cookie 出了问题。

我使用 [ValidateJsonAntiForgeryToken]属性到接收帖子的 Controller 函数。

[HttpPost]
[ValidateJsonAntiForgeryToken]
public JsonResult LoadPage(int id, string returnUrl, PageFormViewModel pageForm, string jsonFormModel)

这是处理 json 验证 token 的代码

[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
public class ValidateJsonAntiForgeryTokenAttribute : FilterAttribute, IAuthorizationFilter
{
    public void OnAuthorization(AuthorizationContext filterContext)
    {
        if (filterContext == null)
        {
            throw new ArgumentNullException("filterContext");
        }

        var httpContext = filterContext.HttpContext;
        var cookie = httpContext.Request.Cookies[AntiForgeryConfig.CookieName];
        AntiForgery.Validate(cookie != null ? cookie.Value : null, httpContext.Request.Headers["__RequestVerificationToken"]);
    }
}

这是我发布它的例子:

var getURL = $('#GetURL').val();
var returnUrl = $('#ReturnUrl').val();
var pageId = $('#PageId').val();
var token = $('input[name="__RequestVerificationToken"]').val();
var headers = {};
headers['__RequestVerificationToken'] = token;

var thePageForm = pageForm;
var theFormModel = formModel;

$.ajax({
    type: 'POST',
    url: getURL,
    contentType: "application/json; charset=utf-8",
    dataType: 'json',
    async: false,
    headers: headers,
    data: JSON.stringify({ id: pageId, returnUrl: returnUrl, pageForm: thePageForm, jsonFormModel: theFormModel }),
    success: function (model) { //Load the page }
    error: function()
});

我有 @Html.AntiForgeryToken()在我看来。实际上我的申请有什么问题?如果问题是用户cookie仍然被系统接受但是无效,那么如何在应用程序重启后重置用户cookie?

----------------------------

更新

看起来代码实际上能够通过 Controller 操作,因此验证 token 没有问题,但是当代码到达尝试从数据库检索数据的行时,发生了这种情况

Message: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)

这是我的连接字符串:

Conn string:Data Source=ip address;Initial Catalog=the database name;Integrated Security=False;User ID=user;Password=the password;MultipleActiveResultSets=True

这是产生错误的代码:

PageViewModel model = new PageViewModel();
model.Page = dbPages.Pages.Where(m => m.Id == id).First();

如果我使用Incognito 模式运行它,它运行良好。那么我的应用程序到底发生了什么?

--------------------

更新 2

经过进一步调试,我现在确定问题出在cookies上。

这些是 cookie:

  1. .ASPXAUTH
  2. ASP.NET_SessionId

我使用"InProc"作为我的 session 状态,应该在应用程序回收/重启后重置,这是我的目标,但我不知道为什么应用程序重启后,cookies仍然存在.

所以我想要的是在服务器重新启动时使所有用户的 session 无效。

解决方案是:

  1. 获取所有用户的所有 session 并使其失效
  2. 在 Application_End() 时注销所有登录用户

根据我的研究,这两种解决方案都是不可能的。请帮忙

最佳答案

我想,this将帮助您设置每次应用程序池回收时触发的事件,然后 this将帮助您使该事件中的所有 cookie 过期。

关于c# - MVC4 : After server restart, 无法发布 ajax 内部服务器错误 500,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41237904/

相关文章:

c# - 如何获取文本字体文件的标题(不是名称)? (TTF 和 OTF)

c# - CookieAuthentication cookie 在应用程序池回收后无效

javascript - jQuery Click 事件 CheckBox 改变 Ajax 调用

asp.net-mvc - 使用客户端证书保护 asp.net mvc 区域

c# - Fluent 验证中的密码验证器

c# - 为什么 datetime 不能比较?

c# - 使用 COM 将数组从 C# 返回到经典 ASP

javascript - 在ajax加载时添加div

javascript - 如何在 Ajax 调用 Controller 后显示 View

asp.net - 如何处理动态创建的表单提交?