防止SQL注入的ASP.NET方法实例解析


当前第2页 返回上一页

2、加入如下代码:

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

void Application_BeginRequest(object sender, EventArgs e)

{

    bool result = false;

    if (Request.RequestType.ToUpper() == "POST")

    {

       //post方式的我就不写了。

    }

    else

    {

      result = ValidUrlGetData();

    }

    if (result)

    {

      Response.Write("您提交的数据有恶意字符!");

      Response.End();

    }

}

/// <summary>

/// 获取QueryString中的数据

/// </summary>

public static bool ValidUrlGetData()

{

    bool result = false;

    for (int i = 0; i < HttpContext.Current.Request.QueryString.Count; i++)

    {

      result = Validate(HttpContext.Current.Request.QueryString[i].ToString());

      if (result)

      {

        break;

      }//如果检测存在漏洞

    }

    return result;

}

public static string []strs = new string[] {"select","drop","exists","exec","insert","delete","update","and","or","user" };//此处我随便加了几个,大家可以多加点哈。

public static bool Validate(string str)

{

    for (int i = 0; i < strs.Length; i++)

    {

      if (str.IndexOf(strs[i]) != -1)

      {

        return true;

        break;

      }

    }

    return false;

}

以上就是防止SQL注入的ASP.NET方法实例解析的详细内容!

返回前面的内容

相关阅读 >>

分享asp.net学习笔记(12)razor 简介

asp.net部署到iis常见问题的解决方法_实用技巧

asp.net中partial class部分类

分享asp.net mvc4如何实现通过id更新表单内容的实例分析

asp.net core新建项目教程(3)_实用技巧

asp.net core项目配置教程(6)_实用技巧

asp.net core razor页面路由的详细介绍

asp.net关于cookie跨域的问题

asp.net webapi中 filter的使用以及执行顺序(收藏)

asp.net性能监控和优化入门

更多相关阅读请进入《asp.net》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...