UTF-8set.。
当我的ASP.NET MVC视图包含以下代码时:
@("<strong>ümlaut</strong>")
然后我希望编码器能逃脱HTML标签,但是
否ümlaut
<strong>ümlaut</strong>
但相反,它给了我以下html:
<strong>ümlaut</strong>
完成完整性,我还提到web.config中的响应编码是对UTF-8的解释,因此我希望HTMLENCODE方法尊重此设置。
<globalization requestEncoding="utf-8" responseEncoding="utf-8" />
是的,我的网页遇到了同样的问题。 如果我们看到htmlencode的代码,则可以将此点转换为一组字符。这是这种字符也翻译的代码。
HTMLENCODE的代码
public static unsafe void HtmlEncode(string value, TextWriter output)
{
if (value != null)
{
if (output == null)
{
throw new ArgumentNullException("output");
}
int num = IndexOfHtmlEncodingChars(value, 0);
if (num == -1)
{
output.Write(value);
}
else
{
int num2 = value.Length - num;
fixed (char* str = ((char*) value))
{
char* chPtr = str;
char* chPtr2 = chPtr;
while (num-- > 0)
{
output.Write(chPtr2[0]);
chPtr2++;
}
while (num2-- > 0)
{
char ch = chPtr2[0];
if (ch <= '>')
{
switch (ch)
{
case '&':
{
output.Write("&");
chPtr2++;
continue;
}
case '\'':
{
output.Write("'");
chPtr2++;
continue;
}
case '"':
{
output.Write(""");
chPtr2++;
continue;
}
case '<':
{
output.Write("<");
chPtr2++;
continue;
}
case '>':
{
output.Write(">");
chPtr2++;
continue;
}
}
output.Write(ch);
chPtr2++;
continue;
}
// !here is the point!
if ((ch >= '\x00a0') && (ch < 'Ā'))
{
output.Write("&#");
output.Write(ch.ToString(NumberFormatInfo.InvariantInfo));
output.Write(';');
}
else
{
output.Write(ch);
}
chPtr2++;
}
}
}
}
}
可能的解决方案是制作您的自定义HTMLENCODE,或使用女士的反交叉网站脚本。
http://msdn.microsoft.com/en-us/security/aa973814
As Aristos建议我们可以使用Microsoft的Antixss库。它包含一个表现出您预期的nunicodecharacterenctercoder
确保我们的内容不会超过UTF-8范围。 我们选择实施我们自己的基本
html编码器。您可以在下面找到代码。如果您看到任何问题,请随时适应/评论/改进。public static class HtmlEncoder
{
private static IDictionary<char, string> toEscape = new Dictionary<char, string>()
{
{ '<', "lt" },
{ '>', "gt" },
{ '"', "quot" },
{ '&', "amp" },
{ '\'', "#39" },
};
/// <summary>
/// HTML-Encodes the provided value
/// </summary>
/// <param name="value">object to encode</param>
/// <returns>An HTML-encoded string representing the provided value.</returns>
public static string Encode(object value)
{
if (value == null)
return string.Empty;
// If value is bare HTML, we expect it to be encoded already
if (value is IHtmlString)
return value.ToString();
string toEncode = value.ToString();
// Init capacity to length of string to encode
var builder = new StringBuilder(toEncode.Length);
foreach (char c in toEncode)
{
string result;
bool success = toEscape.TryGetValue(c, out result);
string character = success
? "&" + result + ";"
: c.ToString();
builder.Append(character);
}
return builder.ToString();
}
}
public static string HtmlEncode(string value,bool removeNewLineAndTabs)
{
if (value == null)
return string.Empty;
string toEncode = value.ToString();
// Init capacity to length of string to encode
var builder = new StringBuilder(toEncode.Length);
foreach (char c in toEncode)
{
string result;
bool success = toEscape.TryGetValue(c, out result);
string character = success ? result : c.ToString();
builder.Append(character);
}
string retVal = builder.ToString();
if (removeNewLineAndTabs)
{
retVal = retVal.Replace("\r\n", " ");
retVal = retVal.Replace("\r", " ");
retVal = retVal.Replace("\n", " ");
retVal = retVal.Replace("\t", " ");
}
return retVal;
}
在我的情况下,在使用“ HTML” DART软件包操纵HTML内容时,我也有类似的问题。但是,通过逃避所有特殊角色,我结合了逃脱的包裹,结合了包装。解决方案是通过以下功能逃脱双重和单句话,即包裹无法逃脱:
String escapeQuotes(String input) {
final toEscape = {
'"': '"',
'\'': ''',
};
final buffer = StringBuffer();
for (var char in input.runes) {
final character = String.fromCharCode(char);
buffer.write(toEscape[character] ?? character);
}
return buffer.toString();
}