2012-03-08 38 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