0

我的网站上有一个带有 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;" })
4

2 回答 2

2

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

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

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

于 2015-04-14T16:54:28.427 回答
0

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

于 2013-11-08T12:09:02.773 回答