Files
zszq-trs/Framework/YLErp.Core/Helpers/HttpHelper.cs
T
hjhan 9bbd512707 fix(auth): 修复JWT令牌登出和刷新中间件问题
- 调整登出流程,先将JWT令牌加入黑名单再删除Cookie
- 在HttpHelper中添加空响应检查,防止接口返回空内容时出错
- 在配额监控服务中增加底层白名单检查的日志记录
- 优化刷新令牌中间件,跳过登录相关页面和静态资源的令牌检查
- 改进令牌失效响应,设置正确的Content-Type并提供多语言提示信息
2026-03-24 13:04:17 +08:00

124 lines
3.9 KiB
C#

using RestSharp;
using RestSharp.Authenticators;
using RestSharp.Authenticators.OAuth2;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using YLErp.DBModels;
namespace YLErp.Helpers
{
public class HttpHelper
{
private string _baseUrl;
private Func<string> _tokenFunc;
public HttpHelper(string baseUrl, Func<string> tokenFunc = null)
{
_baseUrl = baseUrl;
_tokenFunc = tokenFunc;
}
public async Task<TResponse> PostRequest<TRequest, TResponse>(string apiPath, TRequest requestBody)
where TRequest : class
where TResponse : class
{
if (string.IsNullOrEmpty(_baseUrl))
{
throw new ServiceException("Web Base Url未配置");
}
// 创建RestClient
var client = new RestClient(_baseUrl);
if(_tokenFunc!=null)
{
client.Authenticator = new OAuth2AuthorizationRequestHeaderAuthenticator(_tokenFunc(), "Bearer");
}
// 设置token
if (_tokenFunc != null)
{
client.SetJwtToken(_tokenFunc());
}
// 创建请求参数
var request = new RestRequest(apiPath, Method.Post);
if (requestBody != null)
{
request.AddBody(requestBody);
}
// 发送请求并响应结果
var response = await client.ExecuteAsync<ReponseWrap<TResponse>>(request);
return response?.Data?.Data;
}
public async Task<TResponse> PostRequestNoAuth<TRequest, TResponse>(string apiPath, TRequest requestBody)
where TRequest : class
where TResponse : class
{
if (string.IsNullOrEmpty(_baseUrl))
{
throw new ServiceException("Web Base Url未配置");
}
// 创建RestClient
var client = new RestClient(_baseUrl);
// 创建请求参数
var request = new RestRequest(apiPath, Method.Post);
if (requestBody != null)
{
request.AddBody(requestBody);
}
// 发送请求并响应结果
var response = await client.ExecuteAsync<ReponseWrap<TResponse>>(request);
var responseContent = response.Content;
// 检查响应内容是否为空
if (string.IsNullOrEmpty(responseContent))
{
throw new ServiceException($"接口返回为空 - URL: {_baseUrl}{apiPath}, StatusCode: {response.StatusCode}");
}
return Newtonsoft.Json.JsonConvert.DeserializeObject<TResponse>(responseContent);
}
public async Task<TResponse> GetRequestNoAuth<TResponse>(string apiPath)
where TResponse : class
{
if (string.IsNullOrEmpty(_baseUrl))
{
throw new ServiceException("Web Base Url未配置");
}
// 创建 RestClient
var client = new RestClient(_baseUrl);
// 创建请求参数
var request = new RestRequest(apiPath, Method.Get);
// 发送请求并获取响应结果
var response = await client.ExecuteAsync<ReponseWrap<TResponse>>(request);
var responseContent = response.Content;
return Newtonsoft.Json.JsonConvert.DeserializeObject<TResponse>(responseContent);
}
}
public static class RestClientExtension
{
public static void SetJwtToken(this RestClient restClient,string token)
{
restClient.Authenticator = new OAuth2AuthorizationRequestHeaderAuthenticator(token, "Bearer");
}
}
public class ReponseWrap<T>
{
public T Data { get; set; }
}
}