【问题标题】:Session variable value is getting null in ASP.NET Core会话变量值在 ASP.NET Core 中为空
【发布时间】:2018-09-21 01:38:32
【问题描述】:

我正在一种方法中设置会话变量,并尝试从控制器中的另一种方法获取会话变量值,但它总是为空:

这是我的代码:

public class HomeController : Controller
{
    public IActionResult Index()
    { 
        HttpContext.Session.SetString("Test", "Hello!");
        var message = HttpContext.Session.GetString("Test");// Here value is getting correctly
        return View();
    }

    public IActionResult About()
    {
        var message = HttpContext.Session.GetString("Test"); // This value is always getting null here

        return View();
    }
}

这是我在Startup 类中的会话配置:

ConfigureServices() 方法中:

services.Configure<CookiePolicyOptions>(options =>
{
    // This lambda determines whether user consent for non-essential cookies is needed for a given request.
    options.CheckConsentNeeded = context => true;
    options.MinimumSameSitePolicy = SameSiteMode.None;
});

services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddDistributedMemoryCache();
services.AddMvc().AddSessionStateTempDataProvider();
services.AddSession(options =>
{
    options.Cookie.Name = "TanvirArjel.Session";
    options.IdleTimeout = TimeSpan.FromDays(1);
});

Configure() 方法中:

app.UseSession();
app.UseMvc(routes =>
{
    routes.MapRoute(
        name: "default",
        template: "{controller=Home}/{action=Index}/{id?}");
});

很奇怪很奇特的问题!任何帮助将不胜感激!

【问题讨论】:

    标签: session asp.net-core asp.net-core-mvc session-variables


    【解决方案1】:

    对于 ASP.NET Core 2.1 和 2.2

    在Startup类的ConfigureServices方法中,设置options.CheckConsentNeeded = context =&gt; false;如下:

    services.Configure<CookiePolicyOptions>(options =>
    {
      // This lambda determines whether user consent for non-essential cookies is needed for a given request.
      options.CheckConsentNeeded = context => false;
      options.MinimumSameSitePolicy = SameSiteMode.None;
    });
    

    问题解决了!

    【讨论】:

    • 在 asp.net Core 2.1 mvc web 应用程序中为我工作!非常感谢
    • 这对我有用。谢谢你。其他一些人建议将 services.AddMemoryCache() 添加到 ConfigureServices 和 app.UseCookiePolicy() 到 Configure,但这不是必需的。
    • 但这不是表示 CheckConsentNeeded 默认为 false 吗? docs.microsoft.com/en-us/dotnet/api/…
    • 作为布尔属性默认值为false,但在项目模板生成期间默认值设置为true。
    • 不适合我。还有什么我可以做的吗?
    【解决方案2】:

    您也可以只设置Cookie.IsEssential = true,如下所述:https://andrewlock.net/session-state-gdpr-and-non-essential-cookies/

    services.AddSession() 的重载允许您在 Startup 文件中配置 SessionOptions。您可以更改会话超时等各种设置,还可以自定义会话 cookie。要将 cookie 标记为必需,请将 IsEssential 设置为 true:

    public void ConfigureServices(IServiceCollection services)
    {
        services.Configure<CookiePolicyOptions>(options =>
        {
            options.CheckConsentNeeded = context => true; // consent required
            options.MinimumSameSitePolicy = SameSiteMode.None;
        });
    
        services.AddSession(opts => 
        {
            opts.Cookie.IsEssential = true; // make the session cookie Essential
        });
        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
    }
    

    【讨论】:

    • 如果发生上述情况,我会失去控制器之间的价值吗?
    【解决方案3】:

    我遇到了同样的问题,分别尝试了以下方法,我发现它们中的任何一个都对我有用!

     1. options.CheckConsentNeeded = context => false;
     2. opts.Cookie.IsEssential = true; // make the session cookie Essential
    

    不过,虽然不太确定,但我认为 #1 可能会导致违反 GDPR。因此我更喜欢#2。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-21
      • 1970-01-01
      相关资源
      最近更新 更多