具有潛在危險 Request.Form 的值已從用戶端 (ctl00$ContentPlaceHolder1$tbxQuestion=\"<span>ss</span>\") 偵測到
如果想進一步去攔截這訊息,事先把相關錯誤處置好,以免讓 user 看到所謂「當機」或「亂碼」的頁面,則可以在
Global.asax 的 void Application_Error(object sender, EventArgs e) 加上以下程式碼來處理。
void Application_Error(object sender, EventArgs e)
{
// 發生未處理錯誤時執行的程式碼
// At this point we have information about the error
HttpContext ctx = HttpContext.Current;
Exception exception = ctx.Server.GetLastError();
string errorInfo =
"<br>Offending URL: " + ctx.Request.Url.ToString() +
"<br>Source: " + exception.Source +
"<br>Message: " + exception.Message +
"<br>Stack trace: " + exception.StackTrace;
if (exception.StackTrace.Contains("ValidateString"))
{
//因為我所要攔截的訊息會有「ValidateString」字眼,所以我將這事件另外導到其他頁面
ctx.Response.Redirect("Bid_ValidError.aspx?PreUrl=" + ctx.Request.Url.ToString());
}
else
{
ctx.Response.Write(errorInfo);
}
// --------------------------------------------------
// To let the page finish running we clear the error
// --------------------------------------------------
ctx.Server.ClearError();
}
參考資料:
要如何攔截 ASP.Net 防範 XSS 攻擊的錯誤畫面?
ASP.Net Custom Error Pages
沒有留言:
張貼留言