2012-03-08 65 views
1

我仍然是ASP.Net的新手,并没有得到如何解决以下问题。富文本与XSS

我需要丰富的文本发布在我的项目上。我加了NicEdit,因为它好像很容易使用。

因此,如预见,我从服务器得到一个错误。

A potentially dangerous Request.Form value was detected from the client(compteRendu="blablbab<br><br>test<br>"). 

我尝试通过使用htmlencoder来修复它,但是我在使用它时失败了。

我做了什么:

<script type="text/VB"> 
    htmlEncode { 
     model.compteRendu = HtmlEncode(model.compteRendu) 
     } 
</script> 

@Using Html.BeginForm(IsPost) 
    @Html.ValidationSummary(True) 
    @<fieldset> 
     <legend>meeting</legend>  
    @Html.HiddenFor(Function(model) model.idmeeting) 
    <div class="editor-label"> 
     @Html.LabelFor(Function(model) model.compteRendu) 
    </div> 
    <div class="editor-field"> 
     @Html.TextAreaFor(Function(model) model.compteRendu) 
     @Html.ValidationMessageFor(Function(model) model.compteRendu) 
    </div> 
    <p> 
     <input type="submit" value="Save" onclick="htmlEncode"/> 
    </p> 
</fieldset> 
End Using 

所以,我做了什么错?我也tryed做这个控制器内,但我没有发现这是应该的HTML编码

' POST: /Meeting/Edit/5 

    <HttpPost()> 
    Function Edit(meeting As meeting) As ActionResult 
     meeting.compteRendu = HttpEncode(meeting.compteRendu) 
     If ModelState.IsValid Then 
     ... 

PS任何方法:我不是以英语为母语,对不起,如果我的英语很烂。

编辑: 目前,我不需要更多的东西,允许我用
替换我的“新行”。

所以,我发现,我所能做的IIT这样的:

@Html.Raw(meeting.compteRendu.Replace(System.Environment.NewLine, "<br />")) 

目前,它的确定我。但我不确定,也许我需要用颜色创建文本,等等。因此,如果您对如何将验证过的富文本发送到我的数据库有一个想法,我会非常高兴。

+0

这看起来像它可以回答它http://stackoverflow.com/questions/81991/a-potential-dangerous-request-form-value-was-detected-from-the-clients – 2012-03-08 11:25:47

+0

我已经找到/阅读了这个话题。但我对这些解决方案并不满意。 例如:validateRequest =“false” 我也尝试添加: 但它不足以验证没有错误我需要什么。 然后我尝试了他们正在谈论的HtmlEncode,成功... btw,thx时间通过做这项研究花费的时间 – 2012-03-08 11:33:01

回答

3

您可以与<AllowHtml>属性装点您的视图模型的compteRendu属性:

<AllowHtml()> 
Public Property compteRendu As String 

这将在该酒店接受任何字符。里面你的观点,你不需要做任何编码:

@ModelType Meeting 

@Using Html.BeginForm(IsPost) 
    @Html.ValidationSummary(True) 
    @<fieldset> 
     <legend>meeting</legend>  
     @Html.HiddenFor(Function(model) model.idmeeting) 
     <div class="editor-label"> 
      @Html.LabelFor(Function(model) model.compteRendu) 
     </div> 
     <div class="editor-field"> 
      @Html.TextAreaFor(Function(model) model.compteRendu) 
      @Html.ValidationMessageFor(Function(model) model.compteRendu) 
     </div> 
     <p> 
      <input type="submit" value="Save" /> 
     </p> 
</fieldset> 
End Using 

无论你的控制器动作中:

' POST: /Meeting/Edit/5 

<HttpPost()> 
Function Edit(meeting As meeting) As ActionResult 
    If ModelState.IsValid Then 
     ... 
    End If 
    ... 
End Function 
+0

非常感谢,我会在项目中需要进一步的! – 2012-03-09 08:15:08