I want to keep some values in Session as I can access those values in application. It is working fine on localhost. But when I tried to access remotely/from server after publishing, it is not worked. the session value is null.
I am using .NETCoreApp,Version=v2.1 with "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>
Here during running from Visual Studio 2017 it shows in About page Expected Result:
Value: Test Value
But after publishing in windows server 2016 IIS 10.0, it shows in About page Actual Result:
Value:
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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.