【问题标题】:after migrate to .net core 2.0 session stop working correctly迁移到 .net core 2.0 会话后停止正常工作
【发布时间】:2018-03-29 06:47:10
【问题描述】:

我在 .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);

之后我重定向到包含 angular 的静态文件:

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

该文件位于 wwwroot 文件夹中。

当我使用 Angular 从我的应用中获取数据时:

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

我在控制器操作中检查会话:

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

它是空白的。我不知道为什么 - 在更新之前,它工作正常。

【问题讨论】:

  • 您是否使用开发人员工具(网络选项卡)检查了 cookie?您在第一个请求的响应中收到任何 cookie 吗?浏览器是否在请求中将任何 cookie 发送回 data 方法?

标签: c# .net session asp.net-core


【解决方案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/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-02-25
    • 1970-01-01
    • 2020-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-30
    • 2019-08-26
    相关资源
    最近更新 更多