今天启动了一个全新的 ASP.NET Core 网站。按照说明添加会话。我们在索引页上打印出会话 ID,它始终是唯一的。
我认为这可能是 cookie 合规性,所以我在 Chrome 的高级设置和调试器中删除了所有 cookie。但横幅不会再出现让我接受。
我也尝试简单地禁用 CheckConsentNeeded,但这也没有任何影响。
几乎是默认项目加上 MSDN 的重复,除了上面描述的调整之外:
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddDistributedMemoryCache();
services.AddSession(options =>
{
// Set a short timeout for easy testing.
options.IdleTimeout = TimeSpan.FromSeconds(10);
options.Cookie.HttpOnly = true;
//options.Cookie.SecurePolicy = CookieSecurePolicy.Always; //require https
// Make the session cookie essential
options.Cookie.IsEssential = true;
});
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => false; //true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseSession();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
Wiktor Zychla 在第一条评论中是正确的:您必须为要粘贴的 ID 分配任何数据。
我只是将任何数据分配给控制器中的会话:
public IActionResult Index()
{
HttpContext.Session.Set("What", new byte[] { 1, 2, 3, 4, 5 });
}
在那之后,HttpContext.Session.Id
正如人们所期望的那样,并没有改变。
作为我第一次从 ASP.NET Framework 涉足 ASP.NET Core,我没想到会发生这种情况,而且我确信我不会是最后一个!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)