我想在Session中保留一些值,因为我可以在应用程序中访问这些值。在localhost上运行正常。但是,当我尝试在发布后从服务器远程访问/从服务器访问时,它不起作用。会话值为空。
我正在将.NETCoreApp,Version = v2.1与“ Microsoft.AspNetCore.Mvc”一起使用:“ 2.1.1”。
HomeController.cs:
using System;
using System.Diagnostics;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
public class HomeController : Controller
{
public IActionResult Index()
{
HttpContext.Session.SetObjectAsJson("key", "Test value");
return View();
}
public IActionResult About()
{
ViewBag.SessionValue =
HttpContext.Session..GetObjectFromJson<String>("key");
return View();
}
}
About.cshtml:
@{
ViewData["Title"] = "About";
}
<body>
Value: @ViewBag.SessionValue
</body>
在从Visual Studio 2017运行的过程中,此处显示在“关于”页面中 预期结果:
Value: Test Value
但是在Windows Server 2016 IIS 10.0中发布后,它会在“关于”页面中显示 实际结果:
Value:
答案 0 :(得分:0)
You should write options.CheckConsentNeeded = context => false;
in ConfigureServices
.
services.Configure<CookiePolicyOptions>(options =>
{
options.CheckConsentNeeded = context => true; // consent required
options.MinimumSameSitePolicy = SameSiteMode.None;
});
Then add AddSession
before the AddMvc
.
Additionally, to mark the cookie as essential, set IsEssential to true.
services.AddSession(opts =>
{
opts.Cookie.IsEssential = true; // make the session cookie Essential
});
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);