问题描述
在ASP.NET中,添加新闻等页面,用了FreeTextBox后就必须在Web.config中设置pagesvalidateRequest="false"么,这样做是不是不安全?但不设置的话,修改字体颜色等会产生"<>"的操作都会出错。有没有什么方法既可以用FreeTextBox修改字体格式等,又能保证安全?
解决方案
解决方案二:
不一定,你客户端进行先编码也是可以的
解决方案三:
你可以在提交的时候执行下面的代码functionmyfunction(){t=document.getElementById("TextBox1");t.value=escape(t.value);}服务器端Microsoft.JScript.GlobalObject.unescape(TextBox1.Text)得到例子<%@PageLanguage="C#"%><!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><scriptrunat="server">protectedvoidButton1_Click(objectsender,EventArgse){Stringhtml=Microsoft.JScript.GlobalObject.unescape(TextBox1.Text);Response.Write("你输入的是:"+Server.HtmlEncode(html));//为了直接显示才进行HtmlEncode,实际不需要TextBox1.Text=html;}</script><htmlxmlns="http://www.w3.org/1999/xhtml"><headrunat="server"><title></title><script>functionmyfunction(){t=document.getElementById("TextBox1");t.value=escape(t.value);}</script></head><body><formid="form1"runat="server"><asp:TextBoxID="TextBox1"runat="server"Height="275px"TextMode="MultiLine"Width="554px"></asp:TextBox><br/><asp:ButtonID="Button1"runat="server"OnClick="Button1_Click"Text="Button"OnClientClick="myfunction()"/></form></body></html>
解决方案四:
学习了。。。。==================今天下午心情不好,就在CSDN混了。。=========================
解决方案五:
protectedvoidPage_Error(objectsender,EventArgse){Exceptionex=Server.GetLastError();if(HttpContext.Current.Server.GetLastError()isHttpRequestValidationException){HttpContext.Current.Response.Write("请输入合法的字符串【<ahref=/"javascript:history.back(0);/">返回</a>】");HttpContext.Current.Server.ClearError();}}在你的提交事件里面写//将输入字符串编码,这样所有的HTML标签都失效了。StringBuildersb=newStringBuilder(HttpUtility.HtmlEncode(htmlInputTxt.Text));//然后我们选择性的允许<b>和<i>sb.Replace("<b>","<b>");sb.Replace("</b>","</b>");sb.Replace("<i>","<i>");sb.Replace("</i>","</i>");Response.Write(sb.ToString());