Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么我的AJAX请求没有扩展OWIN MVC会话?_C#_Ajax_Asp.net Mvc_Owin_Session Timeout - Fatal编程技术网

C# 为什么我的AJAX请求没有扩展OWIN MVC会话?

C# 为什么我的AJAX请求没有扩展OWIN MVC会话?,c#,ajax,asp.net-mvc,owin,session-timeout,C#,Ajax,Asp.net Mvc,Owin,Session Timeout,我们有一个ASP.NET MVC 5应用程序,它一直在使用带滑动过期的窗体身份验证。我们最近切换到OWIN Cookie身份验证,并且遇到会话扩展不正确的问题 以前,可以从AJAX xhr请求扩展会话。但是,通过这种配置,它们不会扩展。即使在服务器终止会话之后,我也会收到一个200的请求(GET和POST) 当前设置为: app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType

我们有一个ASP.NET MVC 5应用程序,它一直在使用带滑动过期的窗体身份验证。我们最近切换到OWIN Cookie身份验证,并且遇到会话扩展不正确的问题

以前,可以从AJAX xhr请求扩展会话。但是,通过这种配置,它们不会扩展。即使在服务器终止会话之后,我也会收到一个200的请求(GET和POST)

当前设置为:

app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);

UrlHelper url = new UrlHelper(HttpContext.Current.Request.RequestContext);
app.UseCookieAuthentication(new CookieAuthenticationOptions()
{
   AuthenticationType = "Cookies",
   CookieSecure = CookieSecureOption.SameAsRequest,
   CookieName = Constants.CatalystPortalCookieName,
   LoginPath = new PathString(url.Action(nameof(LoginController.Index), "Login")),
   SlidingExpiration = true,
   ExpireTimeSpan = TimeSpan.FromMinutes(20),
   CookiePath = "/",
});

但是,如果我单击一个链接,导致整个页面作为文档响应加载,服务器会正确地扩展会话。

我的一些错误假设就结束了。AJAX请求正在扩展。200的返回让我很失望。在与fiddler一起查看实际响应后,我发现随着对OWIN的更改,401实际上在响应中移动了:

X-Responded-JSON: {"status":401,"headers":{...foo...}}
因此,我们最终只是将响应的状态设置回401。这可能很可怕,但它符合我们的需要

protected void Application_EndRequest() {
    var context = new HttpContextWrapper(Context);
    var header = context.Response.Headers["X-Responded-JSON"];
    if (header != null && header.Contains("\"status\":401")) {
        Context.Response.Clear();
        Context.Response.StatusCode = 401;
    }

这里可能有一个更强大的解决方案:

您是否使用session.about终止会话/注销?如果是这样的话,这不是OWIN的正确方法。对于OWIN,您应该使用AuthenticationManager.SignOut方法如果您查看响应头,是否会看到诸如未设置缓存之类的情况?如果是这样,您可能遇到了cookie冲突问题:打开并检查请求/响应可能是值得的。您如何调用ajax请求?当您使用jQuery指定
缓存时,请确保:false