mvc html.encode,关于asp.net mvc 3:如何在HtmlEncode上为“白名单”输入引号?

大家好!

我正在使用asp.net mvc 3和AntiXssLibrary 4.2,并尝试使用单引号或双引号对某些文本进行编码,问题是我得到了"而不是'或",并且在希伯来语中它们非常有用(例如?? ?"? 要么 ?'?)。 我知道此方法的希伯来语和默认参数中包含:

UnicodeCharacterEncoder.MarkAsSafe(

LowerCodeCharts.Default | LowerCodeCharts.Hebrew,

LowerMidCodeCharts.None,

MidCodeCharts.None,

UpperMidCodeCharts.None,

UpperCodeCharts.None);

我尝试所有编码方法均未获得预期结果。

编辑:

对于我的第二个问题,我试图在我的视图上放置这样的html字符串

return new HtmlString(Encoder.HtmlEncode(resFile));

并且我得到了所有html格式而不是呈现的页面,问题是Microsoft将GetSafeHtml()方法移动到HtmlSanitizationLibrary程序集-在此答案中找到了它,然后从这里下载了它。 现在我可以这样使用

return new HtmlString(Sanitizer.GetSafeHtml(questionsAnswerString));

在那之后,我当然添加了参考

using Microsoft.Security.Application;

现在,我对这些qoutes有所帮助吗?

也许您可以编辑您的问题,说您的预期输出是什么? 如果将最多html标记传递给Encoder.HtmlEncode(),则应该将其标记为html,而不是由浏览器解析和呈现。 请尝试阅读HTML编码和MvcHtmlString.Create,Html.Raw和@,看看是否有帮助。

你是对的! 我尝试像antixss的旧版本一样获得safeHtml

好的,如果您在呈现的html页面上得到",那么在我看来,您正在遇到双重html编码的问题。

要复制您的情况,请在其中一个视图中复制并粘贴Replication:代码,然后亲自查看问题。

HtmlString和MvcHtmlString不应编码已编码的html字符串。因此,就您而言

return new HtmlString(Encoder.HtmlEncode(resFile));

要么

Sanitizer.GetSafeHtml(questionsAnswerString)

返回的是经过Html编码的字符串,然后在视图中实际对它进行了一次编码。

发生这种情况的原因是,在实际渲染内容的视图中,您使用的是剃刀

@alreadyHtmlEncodedString

// razor's @ syntax html encodes the given string

//(irrespective of the fact that the given string is not html encoded

//or the given string is html encoded already or whatever.

//it just encodes the given string)

或aspx

// aspx's html encodes the given string

//(irrespective of the fact that the given string is not html encoded

//or the given string is html encoded already or whatever.

//it just encodes the given string)

So, if that is the case. Either use Html.Raw for the string that is already html encoded. Or just rely on the @ syntax of razor for the unsafe non html encoded string, whichever is your way to go.

复制:

如果有帮助,下面是一些用于复制方案的代码。以及样本输出和图像。将以下代码放入您的视图之一。

@{string quotes = @"'""";

string quotesHtmlEncoded = Html.Encode(@"'""");

string hebrew = @"like ???""? or ?'?";

string hebrewHtmlEncoded = Html.Encode(@"like ???""? or ?'?");

string sampleXss ="alert('1')";

string sampleXssHtmlEncoded = Html.Encode("alert('1')");

}

razor @@RawMvcHtmlString.Create

quotes

@quotes

@Html.Raw(quotes)

@MvcHtmlString.Create(quotes)

quotesHtmlEncoded

@quotesHtmlEncoded

@Html.Raw(quotesHtmlEncoded)

@MvcHtmlString.Create(quotesHtmlEncoded)

hebrew

@hebrew

@Html.Raw(hebrew)

@MvcHtmlString.Create(hebrew)

hebrewHtmlEncoded

@hebrewHtmlEncoded

@Html.Raw(hebrewHtmlEncoded)

@MvcHtmlString.Create(hebrewHtmlEncoded)

sampleXss

@sampleXss

@Html.Raw(sampleXss)

@MvcHtmlString.Create(sampleXss)

sampleXssHtmlEncoded

@sampleXssHtmlEncoded

@Html.Raw(sampleXssHtmlEncoded)

@MvcHtmlString.Create(sampleXssHtmlEncoded)

样本输出图像

0bee58ffa738344430c62e50348c5e22.png

哇..非常感谢您的投入..无论如何,问题出在我们获得视图之前。...在控制器上,我发现引号已被编码文本替换。

很抱歉给您带来麻烦,但无法将这些字符列入白名单。

我们可以在MarkAsSafe的Microsoft参考源上看到野兔。

他打电话给ApplyHtmlSpecificValues(),在那里我们可以看到

private static void ApplyHtmlSpecificValues() {

characterValues['

characterValues['>'] ="gt".ToCharArray();

characterValues['&'] ="amp".ToCharArray();

characterValues['"'] ="quot".ToCharArray();

characterValues['\''] ="#39".ToCharArray();

}

无论如何,它们保留了这些字符,因此您无法在编码后获取它们。

因此,我认为适合调用此函数的唯一解决方案始终是从一个地方开始,并且在执行后只需将字符改回:(

return Encoder.HtmlEncode(input).Replace(""",""").Replace("","'");

10倍;)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值