我刚刚开始使用 AntiXSS (4.3.0),主要是为了使用@Encoder.JavaScriptEncode
如上所述here http://weblogs.asp.net/jongalloway/preventing-javascript-encoding-xss-attacks-in-asp-net-mvc.
我从 Nuget 安装了 AntiXSS,然后添加encoderType="Microsoft.Security.Application.AntiXssEncoder, AntiXssLibrary"
to <httpRuntime
在我的 Web.config 中。
在我看来,我有以下几行(在<script>
tags):
var userId = @Encoder.JavaScriptEncode(User.Identity.GetUserId());
我期望输出
var userId = 'user-id';
但输出:
var userId = 'user-id';
我认为发生这种情况是因为 Razor 正在尝试清理 HTML,从而将单引号编码为'
.
那么解决方案就是将其包裹起来Html.Raw()
,但在我正在关注的帖子 http://weblogs.asp.net/jongalloway/preventing-javascript-encoding-xss-attacks-in-asp-net-mvc他从不这样做(而是在 Javascript 中将整个内容用单引号括起来)。
我的问题是 - 你需要打电话吗@Html.Raw(Encoder.JavaScriptEncode(data))
,还是我的设置有问题?
Thanks!
您对剃刀编码的假设是正确的。我还想说的是您正在关注的帖子 http://weblogs.asp.net/jongalloway/preventing-javascript-encoding-xss-attacks-in-asp-net-mvc也是正确的(我可能是错的,但我还没有阅读整篇文章)。
代替
var userId = @Encoder.JavaScriptEncode(User.Identity.GetUserId());
try
var userId = '@Encoder.JavaScriptEncode(User.Identity.GetUserId(), emitQuotes: false)';
//optionally surround with '' if your userId needs to be a string
or just
var userId = @Encoder.JavaScriptEncode(User.Identity.GetUserId(), emitQuotes: false);
// Visual Studio gives you a red squiggly syntax error after the semi-colon though.
// From your example, if it is a number, then no quotes are required
或者继续Html.Raw()
like
var userId = Html.Raw(@Encoder.JavaScriptEncode(User.Identity.GetUserId());
自以为是:我更喜欢 emitQuotes: false 因为该选项就在那里,并且因为它消除了对另一个函数调用的需要Html.Raw()
。默认为发出报价是真的。你是否错过了发出报价参数还是故意的?
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)