在等待來自前端的確認時嘗試將字串存盤在會話中,但在下一個請求(確認請求)時,會話完全為空。
配置
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddDistributedMemoryCache();
services.AddSession(options => {
options.Cookie.Name = ".SingleTouch.API.Session";
options.IdleTimeout = TimeSpan.FromMinutes(20);
options.Cookie.IsEssential = true;
options.Cookie.HttpOnly = true;
});
string allowedHosts = Configuration.GetValue<string>("AllowedHosts");
services.AddCors(options =>
{
options.AddPolicy(name: CorsAllowedUrls,
builder =>
{
builder.WithOrigins(allowedHosts)
.AllowAnyHeader()
.AllowAnyMethod();
});
});
services.AddControllersWithViews(options =>
options.Filters.Add(new ApiExceptionFilter()));
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseCors(CorsAllowedUrls);
app.UseAuthentication();
app.UseAuthorization();
app.UseCookiePolicy();
app.UseSession();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller}/{action=Index}/{id?}");
});
}
在一個端點我有
_httpContextAccessor.HttpContext.Session.SetString("payEvent", "bro you good?");
并在以下端點上再次檢索它
_httpContextAccessor.HttpContext.Session.GetString("payEvent");
但在檢索時,會話是空的。它肯定是最初添加的,因為如果您在第一個請求中檢查或運行 GetString,則該值存在。Asp 核心 6 正在使用中。
uj5u.com熱心網友回復:
Session 具有范圍生命周期,即對于任何請求,您總是會獲得一個新會話。這是必要的,因為每個請求都可以由不同的用戶發出。如果要存盤特定用戶的資訊,則需要在單例服務中執行此操作。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/473006.html
