简体   繁体   English

在IIS-10中发布后,ASP.NET Core MVC 2.1.0会话无法正常工作

[英]ASP.NET Core MVC 2.1.0 session is not working after publishing in IIS-10

I want to keep some values in Session as I can access those values in application. 我想在Session中保留一些值,因为我可以在应用程序中访问这些值。 It is working fine on localhost. 在localhost上运行正常。 But when I tried to access remotely/from server after publishing, it is not worked. 但是,当我尝试在发布后从服务器远程访问/从服务器访问时,它不起作用。 the session value is null. 会话值是null。

I am using .NETCoreApp,Version=v2.1 with "Microsoft.AspNetCore.Mvc": "2.1.1". 我正在将.NETCoreApp,Version = v2.1与“ Microsoft.AspNetCore.Mvc”:“ 2.1.1”一起使用。

HomeController.cs: 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: About.cshtml:

@{
    ViewData["Title"] = "About";
}
<body>
  Value:    @ViewBag.SessionValue
</body>

Here during running from Visual Studio 2017 it shows in About page Expected Result: 在从Visual Studio 2017运行期间,此处显示在关于页面的预期结果中:

Value: Test Value

But after publishing in windows server 2016 IIS 10.0, it shows in About page Actual Result: 但是在Windows Server 2016 IIS 10.0中发布后,它会在“关于”页面的“实际结果”中显示:

Value:

You should write options.CheckConsentNeeded = context => false; 您应该编写options.CheckConsentNeeded = context => false; in ConfigureServices . ConfigureServices

services.Configure<CookiePolicyOptions>(options =>
{
    options.CheckConsentNeeded = context => true; // consent required
    options.MinimumSameSitePolicy = SameSiteMode.None;
});

Then add AddSession before the AddMvc . 然后在AddSession之前添加AddMvc Additionally, to mark the cookie as essential, set IsEssential to true. 另外,要将cookie标记为必需,请将IsEssential设置为true。

    services.AddSession(opts => 
    {
        opts.Cookie.IsEssential = true; // make the session cookie Essential
    });

    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM