我正在构建一个MVC应用程序。它的行为是如此怪异。如果我在开发服务器上运行它(Visual Studio调试),即使通过更改web.config几次重新启动应用程序,它也可以正常运行,但是当我托管它时,它的行为是如此怪异。
对于第一次加载,一切正常。我可以正常加载所有页面,通过ajax发布等,但是HttpRuntime.UnloadAppDomain();
每次使用ajax发布后,我重新启动服务器后,总能得到
内部服务器错误500
我不知道应用程序正在发生什么,但是我怀疑客户端(用户)缓存/ cookie出了问题。
我将[ValidateJsonAntiForgeryToken]
属性用于接收帖子的控制器功能。
[HttpPost]
[ValidateJsonAntiForgeryToken]
public JsonResult LoadPage(int id, string returnUrl, PageFormViewModel pageForm, string jsonFormModel)
这是处理json验证令牌的代码
[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复位?
更新资料
似乎代码实际上能够通过控制器动作,因此验证令牌没有问题,但是当代码到达尝试从数据库检索数据的行时,就会发生这种情况
消息:建立与SQL Server的连接时发生与网络相关或特定于实例的错误。服务器未找到或无法访问。验证实例名称正确,并且已将SQL Server配置为允许远程连接。(提供者:SQL网络接口,错误:26-指定服务器/实例时出错)
这是我的连接字符串:
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();
如果我使用隐身模式运行它,则可以正常运行。那么我的应用程序到底发生了什么?
更新2
经过进一步调试后,我现在确定问题出在Cookie上。
这些是cookie:
我使用“ InProc”作为会话状态,这是我的目标,应在应用程序回收/重新启动后将其重置,但我不知道为什么在应用程序重新启动后,cookie仍然存在。
因此,如果服务器重新启动,我想要使所有用户的会话无效。
解决方案是:
根据我的研究,这两种解决方案都是不可能的。请帮忙
原因是服务器重启后cookie没有失效。
所以这就是我要做的:
每次服务器重新启动时,服务器都会创建一个cookie,以保存应用程序启动的时间,然后将其传递给所有用户。
在Global.asax“ Application_BeginRequest”中,检查保存应用程序启动时间的cookie,如果该cookie保存较早的时间,则:
WebSecurity.Logout();
Roles.DeleteCookie();
删除所有令人困扰的Cookie。之后,用户将被重定向到登录页面。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句