WYB
2021-03-22 91b8cdad021ab052e4991f3d41834a6f0ddc36b8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using JiepeiWMS.Common;
using JiepeiWMS.Extends;
using JiepeiWMS.Model;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ApplicationModels;
using Microsoft.AspNetCore.Mvc.Authorization;
using Microsoft.AspNetCore.Mvc.Filters;
using System;
using System.Linq;
using System.Threading.Tasks;
 
namespace JiepeiWMS.Filter
{
 
    /// <summary>
    /// 验证拦截器
    /// </summary>
    public class AuthKeySecretAttribute : ActionFilterAttribute
    {
        /// <summary>
        /// 通用的秘钥key
        /// </summary>
        public static string CommonKey = Appsettings.app("AppSettings", "AuthKeySecret").ToString();
 
        /// <summary>
        /// 用户验证
        /// </summary>
        /// <param name="Context"></param>
        public override void OnActionExecuting(ActionExecutingContext Context)
        {   
            var msg = string.Empty;
            var crt = Context.HttpContext;
            var timestamp = crt.Request.Query["Timestamp"].FirstOrDefault()._ToInt64();
 
            var tt = (DateTime.Now - timestamp._ToDateTime()).TotalMinutes;
            if (timestamp == 0 || tt > 5)
            {
                msg = "时间戳已过期";
                goto ErrorResult;
            }
 
            var verifycode = crt.Request.Query["VerifyCode"].FirstOrDefault();
            var md5 = (CommonKey + timestamp)._EcdMD5();
            if (md5 != verifycode)
            {
                msg = "密钥验证失败";
                goto ErrorResult;
            }
            return;
 
        ErrorResult:
            Context.Result = new JsonResult(
                    //该类是KeeSoft框架里自带的一个返回结果集
                    new MessageModel<string>()
                    {
                        success = false,
                        msg = msg
                    }
                );
        }
    }
}