asp.net - 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
}
}
解决方案
推荐阅读
- python - python-quickcheck:找不到错误夹具
- json - 如何在div上显示json响应
- xamarin - Xamarin Forms 在运行时本地化图像未在 iOS 中更新
- python - 获取 IndexError:只有整数、切片 (`:`)、省略号 (`...`)、numpy.newaxis (`None`) 和整数或布尔数组是有效的索引
- deep-learning - 我应该使用哪种图像尺寸来训练 Yolov3?
- laravel - Laravel 7.28.1 [Route: komisis.destroy] [URI: komisis/{komisi}] 缺少必需的参数
- reactjs - 如何在 Typescript 中正确键入 React ErrorBoundary 类组件?
- html - 使用 flexbox 的导航栏消失
- r - 如何按多次出现的值删除行
- html - 如何在引导程序中修改宽度