欢迎您访问我爱IT技术网,今天小编为你分享的编程技术是:【asp.net(C#)防sql注入组件的实现代码】,下面是详细的讲解!
asp.net(C#)防sql注入组件的实现代码
using System;
using System.Configuration;
using System.Web;
using System.Globalization;
namespace JNYW.StuM.SqlInject
{
public class SqlstrAny : IHttpModule
{
public void Init(HttpApplication application)
{
application.BeginRequest +=(new
EventHandler(this.Application_BeginRequest));
}
private void Application_BeginRequest(Object source, EventArgs e)
{
ProcessRequest pr=new ProcessRequest();
pr.StartProcessRequest();
}
public void Dispose()
{
}
}
public class ProcessRequest
{
private static string SqlStr=System.Configuration.ConfigurationManager.AppSettings["SqlInject"].ToString();
private static string sqlErrorPage=System.Configuration.ConfigurationSettings.AppSettings["SQLInjectErrPage"].ToString();
///
/// 用来识别是否是流的方式传输
///
///
///
bool IsUploadRequest(HttpRequest request)
{
return StringStartsWithAnotherIgnoreCase(request.ContentType, "multipart/form-data");
}
///
/// 比较内容类型
///
///
///
///
private static bool StringStartsWithAnotherIgnoreCase(string s1, string s2)
{
return (string.Compare(s1, 0, s2, 0, s2.Length, true, CultureInfo.InvariantCulture)==0);
}
//SQL注入式攻击代码分析
#region SQL注入式攻击代码分析
///
/// 处理用户提交的请求
///
public void StartProcessRequest()
{
HttpRequest Request=System.Web.HttpContext.Current.Request;
HttpResponse Response=System.Web.HttpContext.Current.Response;
try
{
string getkeys="";
if (IsUploadRequest(Request)) return; //如果是流传递就退出
//字符串参数
if (Request.QueryString !=null)
{
for (int i=0; i < Request.QueryString.Count; i++)
{
getkeys=Request.QueryString.Keys[i];
if (!ProcessSqlStr(Request.QueryString[getkeys]))
{
Response.Redirect(sqlErrorPage + "?errmsg=QueryString中含有非法字符串&sqlprocess=true");
Response.End();
}
}
}
//form参数
if (Request.Form !=null)
{
for (int i=0; i < Request.Form.Count; i++)
{
getkeys=Request.Form.Keys[i];
if (!ProcessSqlStr(Request.Form[getkeys]))
{
Response.Redirect(sqlErrorPage + "?errmsg=Form中含有非法字符串&sqlprocess=true");
Response.End();
}
}
}
//cookie参数
if (Request.Cookies !=null)
{
for (int i=0; i < Request.Cookies.Count; i++)
{
getkeys=Request.Cookies.Keys[i];
if (!ProcessSqlStr(Request.Cookies[getkeys].Value))
{
Response.Redirect(sqlErrorPage + "?errmsg=Cookie中含有非法字符串&sqlprocess=true");
Response.End();
}
}
}
}
catch
{
// 错误处理: 处理用户提交信息!
Response.Clear();
Response.Write("CustomErrorPage配置错误");
Response.End();
}
}
///
/// 分析用户请求是否正常
///
/// 传入用户提交数据
/// 返回是否含有SQL注入式攻击代码
private bool ProcessSqlStr(string Str)
{
bool ReturnValue=true;
try
{
if (Str !="")
{
string[] anySqlStr=SqlStr.Split('|');
foreach (string ss in anySqlStr)
{
if (Str.IndexOf(ss) >=0)
{
ReturnValue=false;
break;
}
}
}
}
catch
{
ReturnValue=false;
}
return ReturnValue;
}
#endregion
}
}
关于asp.net(C#)防sql注入组件的实现代码的用户互动如下:
相关问题:请问怎么更好的防止sql注入啊,asp.net c#
答:参照 >>详细
相关问题:asp.net(C#)匹配正则表达,替换所有特殊符号,防止...
答:防止sql注入,最简单的办法就是不要拼接sql,而是采用SqlParameter参数化形式,如果条件可能有可能没有,可以采用: string sql = "select * from xx where 1=1"; if(true){ sql += " and id=@id"; command.Parameters.Add(new SqlParameter } ... >>详细
相关问题:ASP.NET 实现repeater 分页功能,c#
答://在数据访问的时候就分页 数据访问的分页排序方法 int pageId:当前页, int pageSize每页数据数, string addWhere查询条件, string taxisName要排序的列名, string descOrAsc:2种排序参数(desc/asc), string tableName:数据表名 public stati... >>详细
- 【asp】asp.net url重写浅谈-net-url重写
- 【DataSet】DataSet、DataTable、DataRow区别详解
- 【asp】asp.net 动态添加多个用户控件-net-动态添
- 【ASP】ASP.NET中内嵌页面代码的一个问题-NET-内
- 【As】Asp.net中的页面乱码的问题-sp--pn-ne-et
- 【创建】ASP.NET Web API教程 创建域模型的方法详
- 【Asp】Asp.net 页面调用javascript变量的值-net-
- 【ASP】ASP.NET 5升级后如何删除旧版本的DNX-NET5
- 【404页面】ASP.NET设置404页面返回302HTTP状态码
- 【asp】asp.net开发中常见公共捕获异常方式总结(
- 评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
-
