mirror of
https://gitee.com/fudiwei/DotNetCore.SKIT.FlurlHttpClient.Wechat.git
synced 2026-03-10 00:13:36 +08:00
refactor(tenpayv2): 优化签名生成逻辑
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
using System;
|
||||
using JWT;
|
||||
using JWT.Algorithms;
|
||||
using JWT.Serializers;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace SKIT.FlurlHttpClient.Wechat.OpenAI.Utilities
|
||||
{
|
||||
internal static class JwtUtility
|
||||
{
|
||||
private static readonly Lazy<IJwtEncoder> _encoder = new Lazy<IJwtEncoder>(() =>
|
||||
{
|
||||
IJwtAlgorithm algorithm = new HMACSHA256Algorithm();
|
||||
IJsonSerializer serializer = new JsonNetSerializer(new JsonSerializer() { NullValueHandling = NullValueHandling.Ignore });
|
||||
IBase64UrlEncoder urlEncoder = new JwtBase64UrlEncoder();
|
||||
IJwtEncoder encoder = new JwtEncoder(algorithm, serializer, urlEncoder);
|
||||
return encoder;
|
||||
}, isThreadSafe: true);
|
||||
|
||||
public static string EncodeWithHS256(object payload, string secret)
|
||||
{
|
||||
return _encoder.Value.Encode(payload, secret);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
using System.Linq;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System.Text.Json.Nodes;
|
||||
|
||||
namespace SKIT.FlurlHttpClient.Wechat.TenpayV2.Utilities
|
||||
{
|
||||
@@ -13,22 +13,15 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV2.Utilities
|
||||
return string.Empty;
|
||||
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
|
||||
JObject jObject = JsonConvert.DeserializeObject<JObject>(json);
|
||||
JProperty[] jProps = jObject.Properties().OrderBy(p => p.Name).ToArray();
|
||||
foreach (JProperty jProp in jProps)
|
||||
JsonObject jObject = JsonNode.Parse(json)!.AsObject();
|
||||
foreach (KeyValuePair<string, JsonNode?> jProp in jObject.OrderBy(p => p.Key))
|
||||
{
|
||||
if (jProp.Value.Type == JTokenType.Null)
|
||||
{
|
||||
string key = jProp.Key;
|
||||
string? value = jProp.Value?.ToString();
|
||||
if (string.IsNullOrEmpty(value))
|
||||
continue;
|
||||
}
|
||||
else if (jProp.Value.Type == JTokenType.String)
|
||||
{
|
||||
if (string.IsNullOrEmpty((string)jProp.Value))
|
||||
continue;
|
||||
}
|
||||
|
||||
stringBuilder.Append($"{jProp.Name}={jProp.Value}&");
|
||||
stringBuilder.Append($"{key}={value}&");
|
||||
}
|
||||
|
||||
return stringBuilder.ToString().TrimEnd('&');
|
||||
|
||||
Reference in New Issue
Block a user