在引发 System.Web.HttpRequestValidationException 时分析堆栈跟踪,我们可以找出抛出它的代码。
System.Web.HttpRequestValidationException (0x80004005):从客户端检测到潜在危险的 Request.Form 值(IdentifierTextBox="
在 System.Web.HttpRequest.ValidateString(字符串值,字符串 collectionKey,RequestValidationSource requestCollection)
使用 Reflector 我们发现 ValidateString 正在调用:RequestValidator.Current.IsValidRequestString,而后者又调用 CrossSiteScriptingValidation.IsDangerousString,即:
internal static bool IsDangerousString(string s, out int matchIndex)
{
matchIndex = 0;
int startIndex = 0;
while (true)
{
int num2 = s.IndexOfAny(startingChars, startIndex);
if (num2 < 0)
{
return false;
}
if (num2 == (s.Length - 1))
{
return false;
}
matchIndex = num2;
char ch = s[num2];
if (ch != '&')
{
if ((ch == '<') && ((IsAtoZ(s[num2 + 1]) || (s[num2 + 1] == '!')) || ((s[num2 + 1] == '/') || (s[num2 + 1] == '?'))))
{
return true;
}
}
else if (s[num2 + 1] == '#')
{
return true;
}
startIndex = num2 + 1;
}
}