From ecd2829ad660d8ac75a002b10255d29c2b8c5d1d Mon Sep 17 00:00:00 2001 From: Demo <Demo@DESKTOP-CPA90BF> Date: 星期五, 21 六月 2024 16:57:32 +0800 Subject: [PATCH] 测试修改验签问题 --- Wms/Utility/Filter/ApiSignatureVerificationAttribute.cs | 44 +++++++++++++++++++++++++++++++++++++++----- 1 files changed, 39 insertions(+), 5 deletions(-) diff --git a/Wms/Utility/Filter/ApiSignatureVerificationAttribute.cs b/Wms/Utility/Filter/ApiSignatureVerificationAttribute.cs index 286c3bb..1f2adb2 100644 --- a/Wms/Utility/Filter/ApiSignatureVerificationAttribute.cs +++ b/Wms/Utility/Filter/ApiSignatureVerificationAttribute.cs @@ -9,6 +9,8 @@ using System.Threading.Tasks; using Utility.Tools; using static System.Net.Mime.MediaTypeNames; +using Utility.Entity; +using Microsoft.AspNetCore.Http; namespace Utility { @@ -18,11 +20,11 @@ [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false)] public class VerificationAttribute : ActionFilterAttribute { - private static readonly string appKey = SignConfig.AppKey; + private string appKey; private static readonly double Minutes = SignConfig.Minutes;//鏃堕棿鎴冲繀椤�5鍒嗛挓鍐呯殑锛屽惁鍒欎笉閫氳繃 public VerificationAttribute() { - + } public override async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) @@ -30,11 +32,30 @@ var request = context.HttpContext.Request; // 鑾峰彇璇锋眰涓殑鏃堕棿鎴冲拰绛惧悕 + var system = request.Headers["System"].FirstOrDefault(); var timestamp = request.Headers["Timestamp"].FirstOrDefault(); var signature = request.Headers["Signature"].FirstOrDefault(); //var timestamp = "1718873584"; //var signature = "1718873584"; - + switch (system) + { + case "ERP": + appKey = SignConfig.ERPAppKey; + break; + case "MES": + appKey = SignConfig.MESAppKey; + break; + case "LIMS": + appKey = SignConfig.LIMSAppKey; + break; + case "FuMa": + appKey = SignConfig.FuMaAppKey; + break; + default: + context.Result = new UnauthorizedResult(); + return; + } + if (string.IsNullOrEmpty(timestamp) || string.IsNullOrEmpty(signature)) { context.Result = new UnauthorizedResult(); @@ -45,6 +66,17 @@ if (!IsTimestampValid(timestamp)) { context.Result = new UnauthorizedResult(); + var apiResponse = new ApiResponse<object>( + 401, + "error", + "鏃堕棿澶辨晥" + ); + + var json = JsonConvert.SerializeObject(apiResponse); + context.HttpContext.Response.ContentType = "application/json"; + context.HttpContext.Response.ContentLength = Encoding.UTF8.GetByteCount(json); + await context.HttpContext.Response.WriteAsync(json); + await base.OnActionExecutionAsync(context, next); return; } @@ -88,7 +120,6 @@ context.Result = new UnauthorizedResult(); return; } - await base.OnActionExecutionAsync(context, next); } @@ -114,7 +145,10 @@ } public class SignConfig { - public static string AppKey { get; set; } + public static string ERPAppKey { get; set; } + public static string MESAppKey { get; set; } + public static string LIMSAppKey { get; set; } + public static string FuMaAppKey { get; set; } public static double Minutes { get; set; } } -- Gitblit v1.8.0