2017-10-17 103 views
1

我在.NET 1.0中編寫我的應用程序,並在將它更新到2.0版後,我的會話停止工作。停止正常工作

我在Startup.cs設置:

services.AddDistributedMemoryCache(); 
services.AddSession(options => 
{ 
    options.IdleTimeout = TimeSpan.FromMinutes(15); 
    options.Cookie.HttpOnly = true; 
}); 

... 

app.UseSession(); 

我設置會話我的控制器:

HttpContext.Session.SetString(SessionKey, data); 

我重定向到我的靜態文件,其中包含角之後:

return Redirect($"~/index.html?test={test}"); 

該文件被放置在wwwroot文件夾中。

當我使用的角度,從我的應用程序中獲取數據:

$http.get(baseUrl + "/Configure/Refresh?test=" + test).then(handleSuccess, handleError("Error getting settings") 

我在我的控制器行動檢查會話:

_logger.LogInformation($"Session: {HttpContext.Session.GetString(SessionKey)}"); 

而且它是空白。我不知道爲什麼 - 在更新之前,它工作正常。

+1

你檢查與開發工具(網絡選項卡)的餅乾?第一次請求的回覆中是否收到任何cookie?瀏覽器是否將請求中的任何cookie發送回數據方法? –

回答

1

好吧,我發現什麼是錯的。更新會話之後,默認情況下已將SameSite設置爲Lax。以前是沒有。我將此值設置爲Strict,並且所有工作都正常。

services.AddSession(options => 
{ 
    options.IdleTimeout = TimeSpan.FromMinutes(15); 
    options.Cookie.HttpOnly = true; 
    options.Cookie.SameSite = Microsoft.AspNetCore.Http.SameSiteMode.Strict; 
}); 

文章:https://www.sjoerdlangkemper.nl/2016/04/14/preventing-csrf-with-samesite-cookie-attribute/