请求结束后,ASP核心会议eren' t持续

发布于 2025-01-25 22:34:46 字数 2224 浏览 2 评论 0原文

在等待从前端确认的同时,尝试将字符串存储在会话中,但是在下一个请求(确认请求)上,会话完全为空。

配置

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 Core 6正在使用。

Trying to store a string in a session while waiting for confirmation from the front end, but on the next request (confirmation request) the session is completely empty.

Config

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?}");
        });
    }

And in one endpoint I have

_httpContextAccessor.HttpContext.Session.SetString("payEvent", "bro you good?");

and on the the following endpoint it retrieves it again

_httpContextAccessor.HttpContext.Session.GetString("payEvent");

but on retrieval, the session is empty. It's definitely being added initially because if you inspect or run GetString in the first request the value exists. Asp core 6 is in use.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不甘平庸 2025-02-01 22:34:46

该会话范围为终身,即您始终将获得新会话的任何请求。这是Nesessary,因为每个请求都可以由不同的用户发出。如果要为特定用户存储信息,则需要在Singleton服务中进行此操作。

The Session has scoped lifetime, i.e. for any request you always get a new session. This is nesessary as each request can be issued by a differing user. If you want to store information for specific users, you need to do this in a singleton service.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文