我在 IIS 7.5 下运行 ASP.NET Web 应用程序,我的应用程序日志充满了如下错误:
事件代码:3012
事件消息:处理 Web 或脚本资源请求时发生错误。资源标识符解密失败。
...
异常信息:
Exception type: HttpException Exception message: Unable to validate data.
在 System.Web.Configuration.MachineKeySection.EncryptOrDecryptData(布尔 fEncrypt,Byte[] buf,Byte[] 修饰符,Int32 开始,Int32 长度,布尔 useValidationSymAlgo,布尔 useLegacyMode,IVType ivType,布尔 signData)
...
索取资料:
Request URL: http://www.mysite.com/WebResource.axd?d=l0ngstr1ng0fl3tt3rs4ndd1g1ts Request path: /WebResource.axd
...
我怎样才能防止它们出现?根据此链接,我已将以下代码添加到我的 Global.asax 文件中:
void Application_Error(object sender, EventArgs e)
{
// Code that runs when an *unhandled* error occurs
//// get reference to the source of the exception chain
Exception ex = Server.GetLastError();
string message = ex.Message;
string path = Request.Path;
// ignore the following:
// errors due to bots trying AXD URLs
// errors due to <doNastyThings /> tags in the URLs
if (
(ex is HttpException && (path.StartsWith("/WebResource.axd") || path.StartsWith("/ScriptResource.axd"))) ||
(ex is HttpException && message.StartsWith("A potentially dangerous Request.Path value was detected from the client"))
)
{
// clear the error *to prevent it from appearing in the main Application log*
Server.ClearError();
// need to manually direct to the error page, since it will no longer happen automatically once the error has been cleared
Response.Redirect("/Error");
}
}
第二组错误(对于潜在危险的请求)被这段代码捕获和抑制;但是,在执行此代码时,WebResource.axd 错误已写入应用程序日志。我假设这是因为 AXD 处理程序在错误日志记录方面与标准 ASPX 处理程序的工作方式不同(但我不知道该怎么做)。
感谢所有帮助!