gpt4 book ai didi

c# - 在没有 html 注入(inject)的情况下包装到 anchor 标记 2.5 M

转载 作者:行者123 更新时间:2023-11-28 00:21:08 25 4
gpt4 key购买 nike

如何在我的代码中防止 html 注入(inject)问题。

我有将字符串中的所有超链接包装到 anchor 的功能。所以我有这个

@Html.Raw(Helpers.UtilHelpers.ReplaceLinks(Model.Texts[index].Content))

如果没有这个危险的技巧,我怎么能做到这一点?因为通过这种方式,用户可以进行 html 注入(inject)。

我的函数 ReplaceLinks 替换所有超链接,如 http://www.google.com<a href="google.com">http://www.google.com</a> ,所以我想将此行显示为超链接而不是文本。但我只想像这样显示由我的函数生成的标签。

最佳答案

使用 Server.HtmlEncode: http://msdn.microsoft.com/en-us/library/ms525347(v=vs.90).aspx

如果文本包含 html,它将以编码方式显示。

编辑:添加为助手(扩展方法)

public static class HtmlUrlExtension
{
public static string RawUrl( this HtmlHelper html, string url )
{
return html.Raw(HttpServerUtility.HtmlEncode(url));
}
}

并使用它:

@Html.RawUrl(Model.Texts[index].Content)

关于c# - 在没有 html 注入(inject)的情况下包装到 anchor 标记 2.5 M,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8803475/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com