Explorar o código

😁1、升级Furion v4.9.1.30等nuget包版本 2、调整SKIT.FlurlHttpClient.Wechat.Api v3.0版本适配

zuohuaijun %!s(int64=2) %!d(string=hai) anos
pai
achega
63061df284

+ 6 - 6
Admin.NET/Admin.NET.Core/Admin.NET.Core.csproj

@@ -21,9 +21,9 @@
     <PackageReference Include="AspNet.Security.OAuth.Gitee" Version="6.0.15" />
     <PackageReference Include="AspNet.Security.OAuth.Weixin" Version="6.0.15" />
     <PackageReference Include="AspNetCoreRateLimit" Version="5.0.0" />
-    <PackageReference Include="Furion.Extras.Authentication.JwtBearer" Version="4.9.1.29" />
-    <PackageReference Include="Furion.Extras.ObjectMapper.Mapster" Version="4.9.1.29" />
-    <PackageReference Include="Furion.Pure" Version="4.9.1.29" />
+    <PackageReference Include="Furion.Extras.Authentication.JwtBearer" Version="4.9.1.30" />
+    <PackageReference Include="Furion.Extras.ObjectMapper.Mapster" Version="4.9.1.30" />
+    <PackageReference Include="Furion.Pure" Version="4.9.1.30" />
     <PackageReference Include="IPTools.China" Version="1.6.0" />
     <PackageReference Include="Lazy.Captcha.Core" Version="2.0.6" />
     <PackageReference Include="Magicodes.IE.Excel" Version="2.7.5.1" />
@@ -37,9 +37,9 @@
     <PackageReference Include="OnceMi.AspNetCore.OSS" Version="1.1.9" />
     <PackageReference Include="RabbitMQ.Client" Version="6.8.1" />
     <PackageReference Include="SixLabors.ImageSharp.Web" Version="3.1.0" />
-    <PackageReference Include="SKIT.FlurlHttpClient.Wechat.Api" Version="2.37.0" />
-    <PackageReference Include="SKIT.FlurlHttpClient.Wechat.TenpayV3" Version="2.21.0" />
-    <PackageReference Include="SqlSugarCore" Version="5.1.4.140" />
+    <PackageReference Include="SKIT.FlurlHttpClient.Wechat.Api" Version="3.0.0" />
+    <PackageReference Include="SKIT.FlurlHttpClient.Wechat.TenpayV3" Version="3.0.0" />
+    <PackageReference Include="SqlSugarCore" Version="5.1.4.141" />
     <PackageReference Include="System.Linq.Dynamic.Core" Version="1.3.8" />
     <PackageReference Include="UAParser" Version="3.1.47" />
     <PackageReference Include="Yitter.IdGenerator" Version="1.0.14" />

+ 7 - 2
Admin.NET/Admin.NET.Core/OAuth/OAuthSetup.cs

@@ -10,6 +10,7 @@
 using Microsoft.AspNetCore.Authentication;
 using Microsoft.AspNetCore.Authentication.Cookies;
 using Microsoft.AspNetCore.Authentication.JwtBearer;
+using Microsoft.AspNetCore.Builder;
 
 namespace Admin.NET.Core;
 
@@ -29,8 +30,7 @@ public static class OAuthSetup
             })
             .AddCookie(options =>
             {
-                options.Cookie.SameSite = SameSiteMode.None;
-                options.Cookie.SecurePolicy = CookieSecurePolicy.Always;
+                options.Cookie.SameSite = SameSiteMode.Lax;
             })
             .AddWeixin(options =>
             {
@@ -45,4 +45,9 @@ public static class OAuthSetup
                 options.ClaimActions.MapJsonKey(OAuthClaim.GiteeAvatarUrl, "avatar_url");
             });
     }
+
+    public static void UseOAuth(this IApplicationBuilder app)
+    {
+        app.UseCookiePolicy(new CookiePolicyOptions { MinimumSameSitePolicy = SameSiteMode.Lax });
+    }
 }

+ 5 - 5
Admin.NET/Admin.NET.Core/Service/Wechat/SysWechatService.cs

@@ -17,17 +17,17 @@ public class SysWechatService : IDynamicApiController, ITransient
 {
     private readonly SqlSugarRepository<SysWechatUser> _sysWechatUserRep;
     private readonly SysConfigService _sysConfigService;
-    private readonly WechatApiHttpClientFactory _wechatApiHttpClientFactory;
+    private readonly WechatApiClientFactory _wechatApiClientFactory;
     private readonly WechatApiClient _wechatApiClient;
 
     public SysWechatService(SqlSugarRepository<SysWechatUser> sysWechatUserRep,
         SysConfigService sysConfigService,
-        WechatApiHttpClientFactory wechatApiHttpClientFactory)
+        WechatApiClientFactory wechatApiClientFactory)
     {
         _sysWechatUserRep = sysWechatUserRep;
         _sysConfigService = sysConfigService;
-        _wechatApiHttpClientFactory = wechatApiHttpClientFactory;
-        _wechatApiClient = wechatApiHttpClientFactory.CreateWechatClient();
+        _wechatApiClientFactory = wechatApiClientFactory;
+        _wechatApiClient = wechatApiClientFactory.CreateWechatClient();
     }
 
     /// <summary>
@@ -168,7 +168,7 @@ public class SysWechatService : IDynamicApiController, ITransient
             Url = input.Url,
             MiniProgram = new CgibinMessageTemplateSendRequest.Types.MiniProgram
             {
-                AppId = _wechatApiHttpClientFactory._wechatOptions.WechatAppId,
+                AppId = _wechatApiClientFactory._wechatOptions.WechatAppId,
                 PagePath = input.MiniProgramPagePath,
             },
             Data = messageData

+ 2 - 2
Admin.NET/Admin.NET.Core/Service/Wechat/SysWxOpenService.cs

@@ -21,11 +21,11 @@ public class SysWxOpenService : IDynamicApiController, ITransient
 
     public SysWxOpenService(SqlSugarRepository<SysWechatUser> sysWechatUserRep,
         SysConfigService sysConfigService,
-        WechatApiHttpClientFactory wechatApiHttpClientFactory)
+        WechatApiClientFactory wechatApiClientFactory)
     {
         _sysWechatUserRep = sysWechatUserRep;
         _sysConfigService = sysConfigService;
-        _wechatApiClient = wechatApiHttpClientFactory.CreateWxOpenClient();
+        _wechatApiClient = wechatApiClientFactory.CreateWxOpenClient();
     }
 
     /// <summary>

+ 26 - 42
Admin.NET/Admin.NET.Core/Service/Wechat/WechatApiHttpClient.cs

@@ -7,25 +7,22 @@
 // 软件按“原样”提供,不提供任何形式的明示或暗示的保证,包括但不限于对适销性、适用性和非侵权的保证。
 // 在任何情况下,作者或版权持有人均不对任何索赔、损害或其他责任负责,无论是因合同、侵权或其他方式引起的,与软件或其使用或其他交易有关。
 
-using Flurl;
-using Flurl.Http;
-using Flurl.Http.Configuration;
+using Newtonsoft.Json;
 
 namespace Admin.NET.Core.Service;
 
 /// <summary>
 /// 微信API客户端
 /// </summary>
-public partial class WechatApiHttpClientFactory : ISingleton
+public partial class WechatApiClientFactory : ISingleton
 {
+    private readonly IHttpClientFactory _httpClientFactory;
     public readonly WechatOptions _wechatOptions;
 
-    public WechatApiHttpClientFactory(IOptions<WechatOptions> wechatOptions,
-        System.Net.Http.IHttpClientFactory httpClientFactory)
+    public WechatApiClientFactory(IHttpClientFactory httpClientFactory, IOptions<WechatOptions> wechatOptions)
     {
-        _wechatOptions = wechatOptions.Value;
-
-        FlurlHttp.GlobalSettings.FlurlClientFactory = new DelegatingFlurlClientFactory(httpClientFactory);
+        _httpClientFactory = httpClientFactory ?? throw new ArgumentNullException(nameof(httpClientFactory));
+        _wechatOptions = wechatOptions.Value ?? throw new ArgumentNullException(nameof(wechatOptions));
     }
 
     /// <summary>
@@ -37,18 +34,23 @@ public partial class WechatApiHttpClientFactory : ISingleton
         if (string.IsNullOrEmpty(_wechatOptions.WechatAppId) || string.IsNullOrEmpty(_wechatOptions.WechatAppSecret))
             throw Oops.Oh("微信公众号配置错误");
 
-        var wechatApiClient = new WechatApiClient(new WechatApiClientOptions()
+        var client = WechatApiClientBuilder.Create(new WechatApiClientOptions()
         {
             AppId = _wechatOptions.WechatAppId,
             AppSecret = _wechatOptions.WechatAppSecret,
-        });
+        })
+            .UseHttpClient(_httpClientFactory.CreateClient(), disposeClient: false) // 设置 HttpClient 不随客户端一同销毁
+            .Build();
 
-        wechatApiClient.Configure(settings =>
+        client.Configure(config =>
         {
-            settings.JsonSerializer = new FlurlNewtonsoftJsonSerializer();
+            JsonSerializerSettings jsonSerializerSettings = NewtonsoftJsonSerializer.GetDefaultSerializerSettings();
+            jsonSerializerSettings.Formatting = Formatting.Indented;
+            config.JsonSerializer = new NewtonsoftJsonSerializer(jsonSerializerSettings); // 指定 System.Text.Json JSON序列化
+            // config.JsonSerializer = new SystemTextJsonSerializer(jsonSerializerOptions); // 指定 Newtonsoft.Json  JSON序列化
         });
 
-        return wechatApiClient;
+        return client;
     }
 
     /// <summary>
@@ -60,40 +62,22 @@ public partial class WechatApiHttpClientFactory : ISingleton
         if (string.IsNullOrEmpty(_wechatOptions.WxOpenAppId) || string.IsNullOrEmpty(_wechatOptions.WxOpenAppSecret))
             throw Oops.Oh("微信小程序配置错误");
 
-        var WechatApiClient = new WechatApiClient(new WechatApiClientOptions()
+        var client = WechatApiClientBuilder.Create(new WechatApiClientOptions()
         {
             AppId = _wechatOptions.WxOpenAppId,
             AppSecret = _wechatOptions.WxOpenAppSecret
-        });
+        })
+        .UseHttpClient(_httpClientFactory.CreateClient(), disposeClient: false) // 设置 HttpClient 不随客户端一同销毁
+        .Build();
 
-        WechatApiClient.Configure(settings =>
+        client.Configure(config =>
         {
-            settings.JsonSerializer = new FlurlNewtonsoftJsonSerializer();
+            JsonSerializerSettings jsonSerializerSettings = NewtonsoftJsonSerializer.GetDefaultSerializerSettings();
+            jsonSerializerSettings.Formatting = Formatting.Indented;
+            config.JsonSerializer = new NewtonsoftJsonSerializer(jsonSerializerSettings); // 指定 System.Text.Json JSON序列化
+            // config.JsonSerializer = new SystemTextJsonSerializer(jsonSerializerOptions); // 指定 Newtonsoft.Json  JSON序列化
         });
 
-        return WechatApiClient;
-    }
-}
-
-public partial class WechatApiHttpClientFactory
-{
-    internal class DelegatingFlurlClientFactory : IFlurlClientFactory
-    {
-        private readonly System.Net.Http.IHttpClientFactory _httpClientFactory;
-
-        public DelegatingFlurlClientFactory(System.Net.Http.IHttpClientFactory httpClientFactory)
-        {
-            _httpClientFactory = httpClientFactory ?? throw new ArgumentNullException(nameof(httpClientFactory));
-        }
-
-        public IFlurlClient Get(Url url)
-        {
-            return new FlurlClient(_httpClientFactory.CreateClient(url.ToUri().Host));
-        }
-
-        public void Dispose()
-        {
-            // Do Nothing
-        }
+        return client;
     }
 }

+ 3 - 0
Admin.NET/Admin.NET.Web.Core/Startup.cs

@@ -194,6 +194,9 @@ public class Startup : AppStartup
         //// 启用HTTPS
         //app.UseHttpsRedirection();
 
+        // 启用OAuth
+        app.UseOAuth();
+
         // 添加状态码拦截中间件
         app.UseUnifyResultStatusCodes();