现在的位置: 首页 > 综合 > 正文

petshop一个防注入,限制字符长度的方法

2013年10月14日 ⁄ 综合 ⁄ 共 771字 ⁄ 字号 评论关闭

public static string InputText(string inputString, int maxLength) {

StringBuilder retVal = new StringBuilder();

// check incoming parameters for null or blank string
if ((inputString != null) && (inputString != String.Empty)) {
inputString = inputString.Trim();

//chop the string incase the client-side max length
//fields are bypassed to prevent buffer over-runs
if (inputString.Length > maxLength)
inputString = inputString.Substring(0, maxLength);

//convert some harmful symbols incase the regular
//expression validators are changed
for (int i = 0; i < inputString.Length; i++) {
switch (inputString[i]) {
case '"':
retVal.Append("&quot;");
break;
case '<':
retVal.Append("&lt;");
break;
case '>':
retVal.Append("&gt;");
break;
default:
retVal.Append(inputString[i]);
break;
}
}

// Replace single quotes with white space
retVal.Replace("'", "");
}

return retVal.ToString();

}
}
 

抱歉!评论已关闭.