2017-10-17 99 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/