From e8a7f30bd30c594c0040c0db3e39f07b70f90bce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=99=BA=E8=83=BD=E5=A4=A7=E7=9F=B3=E5=A4=B4?= Date: Sat, 11 May 2024 00:31:49 +0800 Subject: [PATCH] =?UTF-8?q?=E9=80=90=E6=AD=A5=E4=BD=BF=E7=94=A8Remoting?= =?UTF-8?q?=E6=9E=B6=E6=9E=84=E6=9B=BF=E4=BB=A3=E5=BA=94=E7=94=A8=E5=B1=82?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Common/BaseController.cs | 8 +++-- Samples/IoTZero/Clients/ClientTest.cs | 33 +++++++++++++++++++ Samples/IoTZero/IoTZero.csproj | 9 ++--- 3 files changed, 43 insertions(+), 7 deletions(-) create mode 100644 Samples/IoTZero/Clients/ClientTest.cs diff --git a/NewLife.Remoting.Extensions/Common/BaseController.cs b/NewLife.Remoting.Extensions/Common/BaseController.cs index 9b1ec49..ad5eb27 100644 --- a/NewLife.Remoting.Extensions/Common/BaseController.cs +++ b/NewLife.Remoting.Extensions/Common/BaseController.cs @@ -8,6 +8,7 @@ using NewLife.Remoting.Extensions.Services; using NewLife.Serialization; using NewLife.Web; +using static NewLife.Remoting.ApiHttpClient; using IWebFilter = Microsoft.AspNetCore.Mvc.Filters.IActionFilter; namespace NewLife.Remoting.Extensions; @@ -57,10 +58,11 @@ void IWebFilter.OnActionExecuting(ActionExecutingContext context) try { - if (context.ActionDescriptor is ControllerActionDescriptor act && !act.MethodInfo.IsDefined(typeof(AllowAnonymousAttribute))) + var rs = !token.IsNullOrEmpty() && OnAuthorize(token); + + if (!rs && context.ActionDescriptor is ControllerActionDescriptor act && !act.MethodInfo.IsDefined(typeof(AllowAnonymousAttribute))) { - var rs = !token.IsNullOrEmpty() && OnAuthorize(token); - if (!rs) throw new ApiException(ApiCode.Forbidden, "认证失败"); + throw new ApiException(ApiCode.Forbidden, "认证失败"); } } catch (Exception ex) diff --git a/Samples/IoTZero/Clients/ClientTest.cs b/Samples/IoTZero/Clients/ClientTest.cs new file mode 100644 index 0000000..5143895 --- /dev/null +++ b/Samples/IoTZero/Clients/ClientTest.cs @@ -0,0 +1,33 @@ +using IoTEdge; +using NewLife.Log; + +namespace IoTZero.Clients; + +/// 客户端测试入口。主程序通过反射调用 +public static class ClientTest +{ + private static ITracer _tracer; + private static HttpDevice _device; + + public static async Task Main(IServiceProvider serviceProvider) + { + await Task.Delay(3_000); + + XTrace.WriteLine("开始IoT客户端测试"); + + _tracer = serviceProvider.GetService(); + + var set = ClientSetting.Current; + + // 产品编码、产品密钥从IoT管理平台获取,设备编码支持自动注册 + var device = new HttpDevice(set) + { + Tracer = _tracer, + Log = XTrace.Log, + }; + + await device.LoginAsync(); + + _device = device; + } +} diff --git a/Samples/IoTZero/IoTZero.csproj b/Samples/IoTZero/IoTZero.csproj index 5ca9251..aa8edb3 100644 --- a/Samples/IoTZero/IoTZero.csproj +++ b/Samples/IoTZero/IoTZero.csproj @@ -40,6 +40,8 @@ + + @@ -48,9 +50,7 @@ - - @@ -66,8 +66,9 @@ - - + + PreserveNewest +