如何在asp.net的文本框中提交HTML标记?

Yus*_*suf 24 javascript c# asp.net-mvc decoding

首先,我想让每个人都知道我使用的是aspx引擎而不是Razor引擎.

我在表格中有一张桌子.我的一个文本框包含html标签

</br>Phone: </br> 814-888-9999 </br> Email: </br> aaa@gmail.com.  
Run Code Online (Sandbox Code Playgroud)

当我去构建它时它会给我一个错误

从客户端检测到潜在危险的Request.Form值(QuestionAnswer="...ics Phone:<br/>814-888-9999<br...").

我尝试了验证request ="false"但它没有用.

对不起,我没有添加我的HTML代码供你查看到目前为止.如果需要的话,我正在提出一些问题,我可以编辑它.

 <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master"   Inherits="System.Web.Mvc.ViewPage<dynamic>" %>


<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
EditFreqQuestionsUser
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<script type="text/javascript">
$(document).ready(function () {
    $("#freqQuestionsUserUpdateButton").click(function () {
        $("#updateFreqQuestionsUser").submit();
    });
});
</script>
<h2>Edit Freq Questions User </h2>

<%Administrator.AdminProductionServices.FreqQuestionsUser freqQuestionsUser =   ViewBag.freqQuestionsUser != null ? ViewBag.freqQuestionsUser : new   Administrator.AdminProductionServices.FreqQuestionsUser(); %>
<%List<string> UserRoleList = Session["UserRoles"] != null ? (List<string>)Session["UserRoles"] : new List<string>(); %>
<form id="updateFreqQuestionsUser" action="<%=Url.Action("SaveFreqQuestionsUser","Prod")%>" method="post" onsubmit+>
<table> 
    <tr>
        <td colspan="3" class="tableHeader">Freq Questions User Details <input type ="hidden" value="<%=freqQuestionsUser.freqQuestionsUserId%>" name="freqQuestionsUserId"/> </td>
    </tr>
     <tr>
        <td colspan="2" class="label">Question Description:</td>
        <td class="content">
            <input type="text" maxlength="2000" name="QuestionDescription" value="  <%=freqQuestionsUser.questionDescription%>" />
        </td>
    </tr>
     <tr>
        <td colspan="2" class="label">QuestionAnswer:</td>
        <td class="content">
            <input type="text" maxlength="2000" name="QuestionAnswer" value="<%=freqQuestionsUser.questionAnswer%>" />
        </td>
    </tr>
    <tr>
        <td colspan="3" class="tableFooter">
                <br />
                <a id="freqQuestionsUserUpdateButton" href="#" class="regularButton">Save</a>
                <a href="javascript:history.back()" class="regularButton">Cancel</a>
        </td> 
    </tr>
    </table>
      </form>
</asp:Content>
Run Code Online (Sandbox Code Playgroud)

Mes*_*esh 29

在提交页面之前,您需要使用window.escape(...)对文本框的值进行html编码

如果您需要服务器端的未转义文本,请使用HttpUtility.UrlDecode(...)方法.

很快的样本:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="SO.WebForm1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script>
        function makeSafe() {
            document.getElementById('TextBox1').value = window.escape(document.getElementById('TextBox1').value);
        };

        function makeDangerous() {
            document.getElementById('TextBox1').value = window.unescape(document.getElementById('TextBox1').value);
        }
    </script>
</head>
<body>
    <form id="form1" runat="server" onsubmit="makeSafe();">
    <div>
        <asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine" Rows="10" ClientIDMode="Static"></asp:TextBox>
    </div>
    <asp:Button ID="Button1" runat="server" Text="Button" />
    </form>


     <script>
         makeDangerous();
    </script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

对代码进行以下更改:

<script type="text/javascript">
    $(document).ready(function () {
        makeDangerous();
        $("#freqQuestionsUserUpdateButton").click(function () {
            makeSafe();
            $("#updateFreqQuestionsUser").submit();
        });
    });

    // Adding an ID attribute to the inputs you want to validate is simplest
    // Better would be to use document.getElementsByTagName and filter the array on NAME
    // or use a JQUERY select....

    function makeSafe() {
        document.getElementById('QuestionAnswer').value = window.escape(document.getElementById('QuestionAnswer').value);
    };

    // In this case adding the HTML back to a textbox should be 'safe'
    // You should be very wary though when you use it as actual HTML
    // You MUST take steps to ensure the HTML is safe.
    function makeDangerous() {
        document.getElementById('QuestionAnswer').value = window.unescape(document.getElementById('QuestionAnswer').value);
    }
</script>
Run Code Online (Sandbox Code Playgroud)


Dar*_*rov 8

使用以下[ValidateInput]属性装饰控制器操作:

[ValidateInput(false)]
[HttpPost]
public ActionResult Foo(MyViewModel model)
{
    ...
}
Run Code Online (Sandbox Code Playgroud)

  • -1:虽然这是一个有效的答案,但这不是一个好的答案.我不认为建议用户盲目禁用验证是个好主意.相反,他们应该确定_why_验证失败,然后提出一个不仅关闭重要安全机制的解决方案.像[Ted Spence](http://stackoverflow.com/a/11673224/56864)和[Adrian](http://stackoverflow.com/a/11673254/56864)提供的建议是一个很好的起点. (11认同)
  • 真的吗?你在争论输入验证是否可笑且不重要?用户输入的"不正当中和"[在今天软件的前25个安全漏洞的前3个中列出](http://www.sans.org/top25-software-errors/).您刚刚告诉该人禁用输入验证,根本没有任何解释.没有解释,没有替代,没有. (6认同)
  • @KevinBabcock,*重要的安全机制*?我对此笑了。您能否解释一下此验证的重要意义是什么?此外,您仅针对此特定操作禁用验证。在 ASP.NET MVC 3 中,您可以通过使用“[AllowHtml]”属性装饰视图模型属性来实现更细粒度的逻辑。在提交之前使用 javascript 转义用户输入有什么意义? (2认同)
  • 在将用户输入提交到服务器之前使用编码用户输入有什么意义?查看[XSS预防规则#2](http://bit.ly/4U6fcT).显然,这只是一线防御,输入也应该在服务器上验证. (2认同)
  • 这很旧,但是@DarinDimitrov是正确的,我知道如何验证自己的输入,在这里我特别希望HTML允许-就像标题中所述。 (2认同)