Alv*_*nus 10 c# cookies ajax asp.net-mvc asp.net-mvc-4
我正在构建一个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)
Run Code Online (Sandbox Code Playgroud)
这是处理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"]);
}
}
Run Code Online (Sandbox Code Playgroud)
这是我发布它的示例:
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()
});
Run Code Online (Sandbox Code Playgroud)
@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();
Run Code Online (Sandbox Code Playgroud)
如果我使用隐身模式运行它,它运行正常.那么我的应用程序究竟发生了什么?
更新2
经过进一步的调试,我现在确定问题在于cookie.
这些是饼干:
我使用"InProc"作为我的会话状态,应该在应用程序回收/重启后重置,这是我的目标,但我不知道为什么在应用程序重启后,cookie仍然存在.
所以我想要的是在重新启动服务器时使所有用户的会话无效.
解决方案是:
从我的研究来看,这两种解决方 请帮忙
故障原因是服务器重启后cookie没有失效。
所以这就是我所做的:
每次服务器重新启动时,服务器都会创建一个cookie来保存应用程序启动的时间,然后将其传递给所有用户。
在Global.asax“Application_BeginRequest”中检查保存应用程序启动时间的cookie,如果cookie保存较早的时间,则:
WebSecurity.Logout();
Roles.DeleteCookie();
Run Code Online (Sandbox Code Playgroud)
删除所有令人烦恼的cookie。之后用户将被重定向到登录页面。
| 归档时间: |
|
| 查看次数: |
699 次 |
| 最近记录: |