首页 后端开发 C#.Net教程 c#如何防止sql注入?

c#如何防止sql注入?

May 10, 2019 pm 05:24 PM
c# 防止sql注入

对于网站的安全性,是每个网站开发者和运营者最关心的问题。网站一旦出现漏洞,那势必将造成很大的损失。为了提高网站的安全性,首先网站要防注入。

c#如何防止sql注入?

下面我们给大家介绍C#防止sql注入的几种方法:

方法一:

在Web.config文件下面增加一个如下标签:

< appSettings>
  < add key="safeParameters" value="OrderID-int32,CustomerEmail-email,ShippingZipcode-USzip" />
< /appSettings>
登录后复制

其中key是 < saveParameters>后面的值为”OrderId-int32”等,其中”-“前面表示参数的名称比如:OrderId,后面的int32表示数据类型。

方法二:

在Global.asax中增加下面一段:

protected void Application_BeginRequest(Object sender, EventArgs e){
  String[] safeParameters = System.Configuration.ConfigurationSettings.AppSettings["safeParameters"].ToString()。Split(&#39;,&#39;);
  for(int i= 0 ;i < safeParameters.Length; i++){
  String parameterName = safeParameters[i].Split(&#39;-&#39;)[0];
  String parameterType = safeParameters[i].Split(&#39;-&#39;)[1];
  isValidParameter(parameterName, parameterType);
  }
  }
  public void isValidParameter(string parameterName, string parameterType){
  string parameterValue = Request.QueryString[parameterName];
  if(parameterValue == null) return;
  if(parameterType.Equals("int32")){
  if(!parameterCheck.isInt(parameterValue)) Response.Redirect("parameterError.aspx");
  }
  else if (parameterType.Equals("USzip")){
  if(!parameterCheck.isUSZip(parameterValue)) Response.Redirect("parameterError.aspx");
  }
  else if (parameterType.Equals("email")){
  if(!parameterCheck.isEmail(parameterValue)) Response.Redirect("parameterError.aspx");
  }
  }
登录后复制

方法三:

使用字符串过滤类

 /**//// < summary>
  /// 处理用户提交的请求
  /// < /summary>
  public static void StartProcessRequest()
  {
  // System.Web.HttpContext.Current.Response.Write("< script>alert(&#39;dddd&#39;);< /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(&#39;请勿非法提交!&#39;);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(&#39;请勿非法提交!&#39;);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 = "&#39;|and|exec|insert|select|delete|update|count|*|chr|mid|master|truncate|char|declare";  bool ReturnValue = true;  try
  {
  if (Str != "")
  {
  string[] anySqlStr = SqlStr.Split(&#39;|&#39;);
  foreach (string ss in anySqlStr)
  {
  if (Str.IndexOf(ss)>=0)
  {
  ReturnValue = false;  }
  }
  }
  }
  catch
  {
  ReturnValue = false;  }
  return ReturnValue;  }
  #endregion  }
  }
登录后复制

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

以上是c#如何防止sql注入?的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

使用 C# 的活动目录 使用 C# 的活动目录 Sep 03, 2024 pm 03:33 PM

使用 C# 的 Active Directory 指南。在这里,我们讨论 Active Directory 在 C# 中的介绍和工作原理以及语法和示例。

C# 中的随机数生成器 C# 中的随机数生成器 Sep 03, 2024 pm 03:34 PM

C# 随机数生成器指南。在这里,我们讨论随机数生成器的工作原理、伪随机数和安全数的概念。

C# 数据网格视图 C# 数据网格视图 Sep 03, 2024 pm 03:32 PM

C# 数据网格视图指南。在这里,我们讨论如何从 SQL 数据库或 Excel 文件加载和导出数据网格视图的示例。

C# 中的阶乘 C# 中的阶乘 Sep 03, 2024 pm 03:34 PM

C# 阶乘指南。这里我们讨论 C# 中阶乘的介绍以及不同的示例和代码实现。

C# 中的模式 C# 中的模式 Sep 03, 2024 pm 03:33 PM

C# 模式指南。在这里,我们讨论 C# 中模式的介绍和前 3 种类型,以及其示例和代码实现。

C# 中的质数 C# 中的质数 Sep 03, 2024 pm 03:35 PM

C# 素数指南。这里我们讨论c#中素数的介绍和示例以及代码实现。

c#多线程和异步的区别 c#多线程和异步的区别 Apr 03, 2025 pm 02:57 PM

多线程和异步的区别在于,多线程同时执行多个线程,而异步在不阻塞当前线程的情况下执行操作。多线程用于计算密集型任务,而异步用于用户交互操作。多线程的优势是提高计算性能,异步的优势是不阻塞 UI 线程。选择多线程还是异步取决于任务性质:计算密集型任务使用多线程,与外部资源交互且需要保持 UI 响应的任务使用异步。

C# 中的队列 C# 中的队列 Sep 03, 2024 pm 03:30 PM

C# 队列指南。这里我们讨论 C# 中队列的介绍、使用不同的示例、构造函数和函数。

See all articles