c#如何防止sql注入?


当前第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

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

/**//// < summary>

 /// 处理用户提交的请求

 /// < /summary>

 public static void StartProcessRequest()

 {

 // System.Web.HttpContext.Current.Response.Write("< script>alert('dddd');< /script>");

 try

 {

 string getkeys = "";  //string sqlErrorPage = System.Configuration.ConfigurationSettings.AppSettings["CustomErrorPage"].ToString();

 if (System.Web.HttpContext.Current.Request.QueryString != null

 {

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

 getkeys = System.Web.HttpContext.Current.Request.QueryString.Keys[i];  if (!ProcessSqlStr(System.Web.HttpContext.Current.Request.QueryString[getkeys],0))

 {

 //System.Web.HttpContext.Current.Response.Redirect (sqlErrorPage+"?errmsg=sqlserver&sqlprocess=true");

 System.Web.HttpContext.Current.Response.Write("< script>alert('请勿非法提交!');history.back();< /script>");

 System.Web.HttpContext.Current.Response.End();

 }

 }

 }

 if (System.Web.HttpContext.Current.Request.Form != null

 {

 for(int i=0;i< System.Web.HttpContext.Current.Request.Form.Count;i++)  {

 getkeys = System.Web.HttpContext.Current.Request.Form.Keys[i];  if (!ProcessSqlStr(System.Web.HttpContext.Current.Request.Form[getkeys],1))

 {

 //System.Web.HttpContext.Current.Response.Redirect (sqlErrorPage+"?errmsg=sqlserver&sqlprocess=true");

 System.Web.HttpContext.Current.Response.Write("< script>alert('请勿非法提交!');history.back();< /script>");

 System.Web.HttpContext.Current.Response.End();

 }

 }

 }

 }

 catch

 {

 // 错误处理: 处理用户提交信息!

 }

 }

 /**//// < summary>

 /// 分析用户请求是否正常

 /// < /summary>

 /// < param name="Str">传入用户提交数据< /param>

 /// < returns>返回是否含有SQL注入式攻击代码< /returns>

 private static bool ProcessSqlStr(string Str,int type)

 {

 string SqlStr;  if(type == 1)

 SqlStr = "exec |insert |select |delete |update |count |chr |mid |master |truncate |char |declare ";  else

 SqlStr = "'|and|exec|insert|select|delete|update|count|*|chr|mid|master|truncate|char|declare";  bool ReturnValue = true;  try

 {

 if (Str != ""

 {

 string[] anySqlStr = SqlStr.Split('|');

 foreach (string ss in anySqlStr)

 {

 if (Str.IndexOf(ss)>=0)

 {

 ReturnValue = false;  }

 }

 }

 }

 catch

 {

 ReturnValue = false;  }

 return ReturnValue;  }

 #endregion  }

 }

相关视频教程推荐:《C#教程》

以上就是c#如何防止sql注入?的详细内容!

返回前面的内容

相关阅读 >>

对比分析C#与java的区别

bitmap生成base64码(C#

详解C#使用xsd文件验证xml格式是否正确的实现方法

C#计算标准偏差相当于excel中的stdev函数的代码案例

使用C#实现发送自定义的html格式邮件的代码案例

页面包含处理实例详解

详细概述C#中的常用字符串方法

C#异常增强

C#开发实例-订制屏幕截图工具(五)针对拖拽时闪烁卡顿的优化

详解C#把unicode编码转换为gb编码的示例代码

更多相关阅读请进入《C#》频道 >>




打赏

取消

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

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

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

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

评论

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