4

我有一个使用 freetextbox 所见即所得编辑器的旧 ASP.NET 应用程序。但它将一个奇怪的 html(不是特定的 html 格式)保存到数据库中。

<TABLE class=mceVisualAid border=0 width=560 align=center height=395>
<TBODY>
<TR align=center>
<TD class=mceVisualAid><SPAN>
<H1 style=COLOR: rgb(0,0,0)    align=center><SPAN><SPAN><SPAN><STRONG><FONT size=3><STRONG><FONT size=3><STRONG><FONT size=2><STRONG><FONT size=3> Message</FONT></STRONG></FONT></STRONG></FONT></STRONG></FONT></STRONG></SPAN></SPAN></SPAN></H1>
<H1 style=COLOR: rgb(0,0,0) align=center><SPAN><SPAN><SPAN><STRONG><FONT size=3><STRONG><FONT size=3><STRONG><FONT size=2><STRONG><FONT size=3>16 August 2013</FONT>

现在我将ckeditor WYSIWYG 用作ASP.net MVC 应用程序,它使用保存在数据库中的相同数据,但我没有找到一种完美的方式将该html 呈现到编辑器中。我的 ckeditor config.js 是:

CKEDITOR.editorConfig = function( config ) {
// Define changes to default configuration here. For example:
// config.language = 'fr';
// config.uiColor = '#AADC6E';
config.entities = false;
config.basicEntities = false;
config.entities_greek = false;
config.entities_latin = false;

};

渲染时显示如下: 在此处输入图像描述

4

2 回答 2

1

尝试在视图中使用它:

@Html.Raw(HttpUtility.HtmlDecode(Model.MyContent)).ToHtmlString();

只需验证 CKEditor 中的输入检查 XSS och 非法标签。

一种方法是使用外部反 XSS 库,在保存到数据库之前,您应该通过消毒剂运行它。重要的是在服务器端进行。

下面只是一个关于反XSS库的建议(不知道有没有更好的东西,因为我很早以前就用过这个)

https://msdn.microsoft.com/en-us/security/aa973814.aspx

于 2016-01-27T06:26:43.717 回答
0

这些是htmlentities。您需要将这些符号转换为真实字符。

在 JS 中执行此操作的一种流行方法是:

var htmlEntities = $('#MyId').ckeditor();   //Or whatever the way you read data 
var pureHtml = $('<textarea />').html(htmlEntities).text();  //Convert

或以下更清洁的方式:

function decodeHTMLEntities (str) {
    if(str && typeof str === 'string') {
      // strip script/html tags
      str = str.replace(/<script[^>]*>([\S\s]*?)<\/script>/gmi, '');
      str = str.replace(/<\/?\w(?:[^"'>]|"[^"]*"|'[^']*')*>/gmi, '');
      element.innerHTML = str;
      str = element.textContent;
      element.textContent = '';
    }

    return str;
  }
于 2016-01-27T06:16:04.940 回答