惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

P
Proofpoint News Feed
博客园 - 聂微东
Application and Cybersecurity Blog
Application and Cybersecurity Blog
MyScale Blog
MyScale Blog
罗磊的独立博客
H
Help Net Security
L
LangChain Blog
T
Threat Research - Cisco Blogs
量子位
S
Securelist
Last Week in AI
Last Week in AI
L
Lohrmann on Cybersecurity
T
The Exploit Database - CXSecurity.com
P
Privacy International News Feed
The Hacker News
The Hacker News
Vercel News
Vercel News
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
Cybersecurity and Infrastructure Security Agency CISA
T
The Blog of Author Tim Ferriss
T
Threatpost
Security Latest
Security Latest
P
Palo Alto Networks Blog
Microsoft Security Blog
Microsoft Security Blog
NISL@THU
NISL@THU
F
Full Disclosure
WordPress大学
WordPress大学
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Stack Overflow Blog
Stack Overflow Blog
C
Check Point Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
酷 壳 – CoolShell
酷 壳 – CoolShell
H
Heimdal Security Blog
J
Java Code Geeks
Recorded Future
Recorded Future
Hugging Face - Blog
Hugging Face - Blog
G
GRAHAM CLULEY
Know Your Adversary
Know Your Adversary
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
阮一峰的网络日志
阮一峰的网络日志
U
Unit 42
B
Blog RSS Feed
月光博客
月光博客
C
Cisco Blogs
V
Visual Studio Blog
D
DataBreaches.Net
H
Hacker News: Front Page
博客园 - 叶小钗
N
News and Events Feed by Topic
爱范儿
爱范儿
A
Arctic Wolf

博客园 - javalong

VS2005 中通过MasterPage来更方便实现网站模板替换 不走寻常路 设计ASP.NET应用程序的七大绝招 进制转换(一) 关于ISA2004内网卡网线拔插造成ISA代理失效问题的一种解决方法(下列错误而失败:0x80072 关于三层结构的一点使用心得与开发建议 さくら~歌詞 いきものがかり SAKURA 歌词 修改数据库逻辑名 Windows 2003 R2 一些SN map blog - javalong - 博客园 VB.Net to C# Converter 1.51 图书馆 2006年宜结婚嫁娶黄道吉日通用表 VS2005 发布网站 真的好麻烦!!! 关于URL重写使用正则表达式的一些问题! 使用 ASP.NET 2.0 ObjectDataSource 控件 12月16日参加了传说中的微软新产品发布会!----广州 工厂的资料 数据访问 SimplePager
.Net程序如何防止被注入(整站通用)
javalong · 2006-11-23 · via 博客园 - javalong

防止sql注入,通常一个一个文件修改不仅麻烦而且还有漏掉的危险,下面我说一上如何从整个系统防止注入。

做到以下三步,相信的程序将会比较安全了,而且对整个网站的维护也将会变的简单。

一、数据验证类:
parameterCheck.cs

public class parameterCheck{
 public static bool isEmail(string emailString){
 return System.Text.RegularExpressions.Regex.IsMatch(emailString, "['//w_-]+(//.['//w_-]+)*@['//w_-]+(//.['//w_-]+)*//.[a-zA-Z]{2,4}");
 }
 public static bool isInt(string intString){
 return System.Text.RegularExpressions.Regex.IsMatch(intString ,"^(//d{5}-//d{4})|(//d{5})$");
 }
 public static bool isUSZip(string zipString){
 return System.Text.RegularExpressions.Regex.IsMatch(zipString ,"^-[0-9]+$|^[0-9]+$");
 }
}

二、Web.config

在你的Web.config文件中,在<appSettings>下面增加一个标签:如下

 <appSettings>
 <add key="safeParameters" value="OrderID-int32,CustomerEmail-email,ShippingZipcode-USzip" />
</appSettings>

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

三、Global.asax

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

protected void Application_BeginRequest(Object sender, EventArgs e){
 String[] safeParameters = System.Configuration.ConfigurationSettings.AppSettings["safeParameters"].ToString().Split(',');
 for(int i= 0 ;i < safeParameters.Length; i++){
 String parameterName = safeParameters[i].Split('-')[0];
 String parameterType = safeParameters[i].Split('-')[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("double")){
 if(!parameterCheck.isDouble(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");
 }
}

以后需要修改的时候我们只需要修改以上三个文件,对整个系统的维护将会大大提高效率,当然你可以根据自己的需要增加其它的变量参数和数据类型。