2015-10-16 73 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

相關問題