As long as the following three points, the site will be more secure and easy to maintain.
first, data validation class
Copy Code code as follows:
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]
+$");
}
}
Second, web.config
In your Web.config file, add a label below, as follows:
Copy Code code as follows:
<appSettings>
<add key= "Safeparameters" value= "orderid-int32,customeremail-email,shippingzipcode-
Uszip "/>
</appSettings>
Where key is followed by the value "Orderid-int32", where "-" preceded by the name of the parameter such as: OrderId, followed by the int32 represents the data type.
Third, Global.asax
Add the following paragraph to the Global.asax:
Copy Code code as follows:
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");
}
}
Later need to modify the time you can only modify the above three files on it, the overall system maintenance efficiency will improve, of course, you can also according to their own needs to add other variables parameters and data types and so on.