2015-10-16 71 views
12

我们有一个ASP.NET MVC 5应用程序,该应用程序一直使用带有过期失效的表单身份验证。我们最近切换到OWIN Cookie身份验证,并且遇到了我们的会话没有正确扩展的问题。为什么我的AJAX请求不能扩展OWIN MVC会话?

以前,会话可以从AJAX xhr请求扩展。但是,通过这种配置,它们不会延伸。即使在服务器已经终止会话之后,对于每个请求(包括GET和POST),我都会收到200个应该扩展的请求。

目前的设置是:

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 = "/", 
}); 

如果我点击,导致整个页面被加载为响应文件的链接,但是,服务器适当延长会议。

+1

您使用* session.abandon *杀死会话/注销?如果这样的话与OWIN不正确的话。使用OWIN,你应该使用AuthenticationManager.SignOut方法 –

+1

如果你看看响应头文件,你会看到诸如no-cache set之类的东西吗?如果是这样,你可能会碰到cookie冲突问题:http://katanaproject.codeplex.com/wikipage?title=System.Web%20response%20cookie%20integration%20issues – Tratcher

+1

它可能是值得开裂[fiddler](http:///www.telerik.com/fiddler)并检查请求/响应。 –

回答

0

我结束了一些错误的假设。 AJAX请求正在扩展。 200的回归让我失望。与小提琴手在实际反应看后,我看到了与变化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; 
    } 

这里可能是一个更强大的解决方案:OWIN: unauthorised webapi call returning login page rather than 401

相关问题