username@email.com
2025-04-27 15eb82df2d6ec539e9d4245bfe08d531e8eb6379
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
63
64
65
using CommonToolsCore;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace zhengcaioa.Model
{
    public class AntiSqlAttribute: Attribute, IActionFilter
    {
        public void OnActionExecuted(ActionExecutedContext context)//方法执行后执行
        {
 
        }
 
        public void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var actionParameters = filterContext.ActionDescriptor.Parameters;
            foreach (var p in actionParameters)
            {
                if (p.Name == "file")
                {
                    continue;
                }
                if (p.ParameterType == typeof(string))
                {
                    if (filterContext.ActionArguments.ContainsKey(p.Name) && filterContext.ActionArguments[p.Name] != null)
                    {
                        filterContext.ActionArguments[p.Name] = SqlFilter.SqlFilterFilter(filterContext.ActionArguments[p.Name].ToString().Trim()) ;
                    }
                }
                else
                {
                    var model = filterContext.ActionArguments[p.Name];
                    Type type = model.GetType();
                    foreach (var item in type.GetProperties())
                    {
                        if (item.PropertyType == typeof(string) && item.GetValue(model, null) != null)
                        {
                            if (!item.IsDefined(typeof(IgnoreSqlInjectAttribute), false))
                            {
                                item.SetValue(model, SqlFilter.SqlFilterFilter(item.GetValue(model, null).ToString().Trim()), null);
                            }
                        }
                    }
                }
            }
 
        }
    }
    /// <summary>
    /// 忽略SQL注入
    /// [IgnoreSqlInject]
    /// </summary>
    [AttributeUsageAttribute(AttributeTargets.Class | AttributeTargets.Property, AllowMultiple = true)]
    public class IgnoreSqlInjectAttribute : Attribute
    {
 
    }
 
}