HTML encoding issue in asp.net

风流意气都作罢 提交于 2019-12-25 03:56:51

问题


I have a literal control being used to display HTML coming from DB. I did face some XSS issues and implemented Anti-XSS Security Runtime Engine (SRE) to automatically encode all html markup. e.g.

DB : <p align="center"></p>

Anti-XSS encodes it as :

&#60;p align&#61;&#34;center&#34;&#62;&#160;&#60;&#47;p&#62

However, when I am setting text property of literal content from code behind, I was expecting that the literal control will DECODE the proper html and display the rendered version. Instead, it is showing the ENCODED version.

Thus literal control displays - <p align="center"></p> postrender. I understand it is Anti-xss in action but how can I get the literal control to show the rendered HTML instead of markup?

ASPX - <asp:Literal ID="ltPageContent" runat="server"></asp:Literal>
Code behind on page load - ltPageContent.Text = getPageContent("home")'Gets HTML from DB

Am I missing something simple here?


回答1:


Without considering XSS risks, you may forget LiteralControl here and use inline codes instead:

ASPX:

<%= Server.HtmlDecode(YOUR_STRING) %>



回答2:


You can also use the "Mode" property with a value of "PassThrough":

<asp:Literal ID="ltPageContent" runat="server" Text="Html Here" 
             Mode="PassThrough" />

I do advise to check for XSS before data is passed here though.



来源:https://stackoverflow.com/questions/7695148/html-encoding-issue-in-asp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!