首页 > 解决方案 > httpClient 异步请求和会话

问题描述

使用 httpClient 检查会话是否处于活动状态或不总是返回 null,我在 js 上测试了使用 WithCredential:true 子句发出异步请求,并按预期工作,当会话处于活动状态时返回 true,否则返回 false。

我已经完成了 cors 配置(在 js 上有效),它是:

string[] origins = new string[2] { "http://subscripciones:8080", "http://localhost:5000" };
        services.AddCors(options =>
        {
            options.AddPolicy("AllowMyOrigin",
            builder => builder.WithOrigins(origins)
                              .AllowAnyHeader()
                              .AllowAnyMethod()
                              .AllowCredentials());
        });

这是我的会话配置:

            services.AddDistributedMemoryCache();
        services.AddSession(options =>
        {
            // Set a short timeout for easy testing.
            options.IdleTimeout = TimeSpan.FromSeconds(60);
            options.Cookie.SecurePolicy = CookieSecurePolicy.Always;
            options.Cookie.HttpOnly = true;
            // Make the session cookie essential
            options.Cookie.IsEssential = true;
        });

//and app.UseSession(new SessionOptions() { Cookie = new CookieBuilder() { Name = ".AspNetCore.Session.ProduSSO" } });

这是我通过 httpclient 发出异步请求的配置:

       private readonly HttpClient clientSSO = new HttpClient(new HttpClientHandler
       {
         UseDefaultCredentials = true,
          ClientCertificateOptions = ClientCertificateOption.Automatic
       });
         var httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, "http://localhost:5291/Security/IsSessionActive");
        httpRequestMessage.Headers.Add("Host", "localhost");
        httpRequestMessage.Headers.Add("Connection", "keep-alive");
        httpRequestMessage.Headers.Add("Pragma", "no-cache");
        httpRequestMessage.Headers.Add("Cache-Control", "no-cache");
        httpRequestMessage.Headers.Add("Access-Control-Request-Method", "GET");
        httpRequestMessage.Headers.Add("Origin", "http://localhost:5000");
        httpRequestMessage.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36");
        httpRequestMessage.Headers.Add("Access-Control-Request-Headers", "authorization");
        httpRequestMessage.Headers.Add("Accept", "*/*");
        httpRequestMessage.Headers.Add("Sec-Fetch-Site", "cross-site");
        httpRequestMessage.Headers.Add("Sec-Fetch-Mode", "cors");
        httpRequestMessage.Headers.Add("Referer", "http://locahost:5000/");
        httpRequestMessage.Headers.Add("Accept-Encoding", "gzip, deflate, br");
        httpRequestMessage.Headers.Add("Accept-Language", "es-ES,es;q=0.9,en;q=0.8,pt;q=0.7");
        httpRequestMessage.Headers.Add("Authorization", "Bearer 1Q7zkOL59cRqWBkQ12ZiGVW2DBL");

        using (var response = await this.clientSSO.SendAsync(httpRequestMessage))
        {
            if (response.StatusCode == HttpStatusCode.OK)
            {
                var result = response.StatusCode == HttpStatusCode.OK ? await response.Content.ReadAsStringAsync() : String.Empty; // always false

            }
        }

标签: asp.netasp.net-corehttpclient

解决方案


推荐阅读