ugu*_*gur 14 asp.net asp.net-core-2.1
在ASP.NET核心2.1中,我无法访问会话变量.
在调试时我注意到在每个请求中会话ID都会更改(HttpContex.Session.Id)
我在会话配置中犯了错误吗?
Startup.cs
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
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);
// Adds a default in-memory implementation of IDistributedCache.
services.AddDistributedMemoryCache();
services.AddSession(options =>
{
// Set a short timeout for easy testing.
options.IdleTimeout = TimeSpan.FromSeconds(1000);
options.Cookie.HttpOnly = true;
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseSession();
if (env.IsDevelopment())
{
app.UseBrowserLink();
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
}
Run Code Online (Sandbox Code Playgroud)
Program.cs中
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
}
Run Code Online (Sandbox Code Playgroud)
在调试时我注意到在每个请求中会话ID都发生了变化(HttpContex.Session.Id)
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using ucms6.Models;
namespace ucms6.Controllers
{
public class HomeController : Controller
{
const string SessionKeyName = "_Name";
const string SessionKeyYearsMember = "_YearsMember";
const string SessionKeyDate = "_Date";
public IActionResult Index()
{
// Requires using Microsoft.AspNetCore.Http;
HttpContext.Session.SetString(SessionKeyName, "Rick");
HttpContext.Session.SetInt32(SessionKeyYearsMember, 3);
return RedirectToAction("SessionNameYears");
// return View();
}
public IActionResult SessionNameYears()
{
var name = HttpContext.Session.GetString(SessionKeyName);
var yearsMember = HttpContext.Session.GetInt32(SessionKeyYearsMember);
return Content($"Name: \"{name}\", Membership years: \"{yearsMember}\"");
}
public IActionResult About()
{
ViewData["Message"] = "Your application description page.";
return View();
}
Run Code Online (Sandbox Code Playgroud)
Tan*_*jel 23
在类的ConfigureServices()方法中Startup,设置options.CheckConsentNeeded = context => 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; // Default is true, make it false
options.MinimumSameSitePolicy = SameSiteMode.None;
});
Run Code Online (Sandbox Code Playgroud)
解决方案是将会话cookie标记为必要.
public void ConfigureServices(IServiceCollection services)
{
//...
services.AddSession(opt =>
{
opt.Cookie.IsEssential = true;
});
//...
}
Run Code Online (Sandbox Code Playgroud)
有关旗帜的文件说明:
指示此cookie是否对于应用程序正常运行至关重要.如果为真,则可以绕过同意政策检查.默认值为false.
这将保持cookie策略选项不变,并且会话仍然按预期工作,因为CookiePolicyOptions.CheckConsentNeeded只影响非必要的cookie.
ASP.NET Core 中默认的分布式缓存存储位于内存中。由于会话使用分布式缓存,这意味着您的会话存储也在内存中。存储在内存中的内容是与进程绑定的,因此如果进程终止,存储在内存中的所有内容都会随之终止。最后,当您停止调试时,应用程序进程将终止。这意味着每次启动和停止调试时,您都会有一个全新的会话存储。
您可以选择几条路线。首先,如果您只想运行站点而不进行调试,则可以使用CTRL+ F5。这将启动 IIS Express 并加载您的 Web 应用程序,而无需启动所有调试机制。然后,您可以继续发出任意数量的请求,并且所有请求都将到达同一进程(这意味着您的会话存储将完好无损)。这对于进行前端开发非常有用,因为您可以修改 Razor 视图、CSS、JS 等并查看这些更改,而无需停止并再次开始调试。但是,如果您进行任何 C# 代码更改(类、控制器等),Visual Studio 将启动构建,这将终止应用程序,然后重新启动它。您的网站继续运行,就像什么都没发生一样,但内存中存储的任何内容(包括您的会话)都将消失。不过,这至少比不断调试要好。
其次,您也可以在开发中简单地使用持久性存储(您应该已经设置为在生产中使用持久性存储,因此请尽快修复该问题,如果没有)。您可以在开发中使用 SQL Server 或 Redis 等工具,就像在生产中一样。SQL 存储可以添加到您现有的开发数据库中,因此您实际上不需要安装 SQL Server。如果您更喜欢这种方式,您还可以安装 Redis 的本地副本,然后在本地主机上运行它。无论采用哪种方法,您的分布式缓存以及会话都将存储在应用程序外部的某个位置,因此启动和停止应用程序不会影响存储在其中的内容。
| 归档时间: |
|
| 查看次数: |
13629 次 |
| 最近记录: |