【问题标题】:MVC c# prevent xssMVC c# 防止xss
【发布时间】:2013-11-08 12:05:42
【问题描述】:

我的网站上有一个使用 mvc c# 的简单电子邮件表单。

如果我添加到文本框 alert("test") 我得到以下异常:

A potentially dangerous Request.Form value was detected from the client (Message="<script>alert("test"...").

我不希望用户能够插入 javascript。我需要 html 编码我会在这个字段上这样做

 @Html.TextAreaFor(model => model.Message, new { @style = "width:800px;height:300px;" })

【问题讨论】:

标签: asp.net-mvc xss


【解决方案1】:

选项 1:查看接受的答案: HTML-encoding lost when attribute read from input field

选项 2:将 [AllowHtml] 属性放在绑定到此文本框的模型项上,这将使值进入您可以使用 HtmlEncode 的控制器。

选项 3:将 [ValidateInput(false)] 属性放在您的控制器操作上,这让一切都通过,然后您可以对所有内容进行自定义验证

【讨论】:

    【解决方案2】:

    使用 System.Web.HttpUtility.HtmlEncode 对所有用户输入进行编码并避免 XSS 攻击。

    【讨论】:

    • 如何通过@Html.TextAreaFor 将其添加到控件中
    猜你喜欢
    • 2017-06-15
    • 2012-10-28
    • 2018-09-12
    • 2022-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多