简体   繁体   English

迁移到.net core 2.0会话后停止正常工作

[英]after migrate to .net core 2.0 session stop working correctly

I write my application in .NET 1.0 and after an update it to version 2.0 then, my session stopped working. 我在.NET 1.0中编写应用程序,然后将其更新到2.0版本后,我的会话停止工作。

My settings in Startup.cs: 我在Startup.cs中的设置:

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

...

app.UseSession();

I set the session at my controller: 我在我的控制器上设置了会话:

HttpContext.Session.SetString(SessionKey, data);

After that I redirect to my static file containing angular: 之后,我重定向到包含angular的静态文件:

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

The file is placed in the wwwroot folder. 该文件放在wwwroot文件夹中。

And when I use angular to get data from my app: 当我使用angular从我的应用程序获取数据时:

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

I check the session in my controller action: 我在控制器操作中检查会话:

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

And it is blank. 它是空白的。 I don't know why - before the update, it worked correctly. 我不知道为什么 - 在更新之前,它工作正常。

Ok I discover what was wrong. 好的,我发现了什么问题。 After update session as default have SameSite set to Lax. 默认情况下,更新会话后将SameSite设置为Lax。 Before is was none. 之前是没有。 I set this value to Strict and all work correctly. 我将此值设置为Strict,并且所有工作都正常。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用System.IdentityModel.Tokens.Jwt从5.1迁移到2.0后,JWTAuthentication无法在asp.net core 2.0中运行 - 5.1.4更新 - JWTAuthentication not working in asp.net core 2.0 after migrate from 1.1 to 2.0 with System.IdentityModel.Tokens.Jwt - 5.1.4 update 将.Net Standard 2.0 迁移到.Net Core 2.2 - Migrate .Net Standard 2.0 to .Net Core 2.2 在.Net Core 2.0中使用会话 - Using Session in .Net Core 2.0 Azure Ad .net core 2.0的会话超时 - Session timeout with azure Ad .net core 2.0 将代码 2.0 迁移到 3.1 核心代码,然后 swagger api versing 不起作用 - Migrate code 2.0 to 3.1 core code then swagger api versing not working Asp.net Core 2.0从发布的1.x迁移到Azure无法正常工作HTTP错误502.5-进程失败 - Asp.net core 2.0 migrate from 1.x published to Azure not working HTTP Error 502.5 - Process Failure ASP.NET Core 2.0身份验证不起作用 - ASP.NET Core 2.0 Authentication NOT Working Asp.net Core 2.0中的本地化无法正常工作 - Localisation in Asp.net Core 2.0 not working 从ASP.NET Core 1.1 MVC迁移到2.0后,自定义cookie身份验证无法正常工作 - Custom cookie authentication not working after migration from ASP.NET Core 1.1 MVC to 2.0 在 IFrame 的 ASP.NET Core 2.0 应用程序中使用会话状态 - Using session state in ASP.NET Core 2.0 app in IFrame
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM