| | |
| | | using System.Threading.Tasks; |
| | | using Utility.Tools; |
| | | using static System.Net.Mime.MediaTypeNames; |
| | | using Utility.Entity; |
| | | using Microsoft.AspNetCore.Http; |
| | | |
| | | namespace Utility |
| | | { |
| | |
| | | [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) |
| | |
| | | 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(); |
| | |
| | | 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; |
| | | } |
| | | |
| | |
| | | context.Result = new UnauthorizedResult(); |
| | | return; |
| | | } |
| | | |
| | | await base.OnActionExecutionAsync(context, next); |
| | | } |
| | | |
| | |
| | | } |
| | | 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; } |
| | | } |
| | | |