这些跨站点脚本修复是如何工作的?

How do these cross site scripting fixes work?

我的代码应该已经清除了跨站点脚本漏洞。

例如:

<%# Eval("Name") %> (<%# Eval(" FriendlyId") %>)

修改为:

<%#: Eval("Name") %> (<%# Eval(" FriendlyId") %>)

<%= string.IsNullOrEmpty(SelectedPlan.VisionPlan.PlanCategory) ? "-" : SelectedPlan.VisionPlan.PlanCategory %>

修改为:

<%: string.IsNullOrEmpty(SelectedPlan.VisionPlan.PlanCategory) ? "-" : SelectedPlan.VisionPlan.PlanCategory %>

如何简单地添加这些冒号来防止跨站点脚本攻击的发生?

<%: > 是 ASP.NET 4 中引入的 HTML 编码输出的新语法。如果没有此语法,您将不得不编写

<%= Server.HtmlEncode(Model.Content) %>

可以使用新语法以更具可读性和简洁的方式表达同样的内容:

<%: Model.Content %>

Scott Guthrie 解释了更多细节:

The new <%: %> syntax provides a concise way to automatically HTML encode content and then render it as output. It allows you to make your code a little less verbose, and to easily check/verify that you are always HTML encoding content throughout your site. This can help protect your applications against cross-site script injection (XSS) and HTML injection attacks.

有关详细信息,请参阅 New <%: %> Syntax for HTML Encoding Output in ASP.NET 4 (and ASP.NET MVC 2)