用了FreeTextBox后 pages validateRequest="false"的问题
在ASP.NET中,添加新闻等页面,用了FreeTextBox后就必须在Web.config中设置 pages validateRequest="false"么,这样做是不是不安全?但不设置的话,修改字体颜色等会产生"<>"的操作都会出错。有没有什么方法既可以用FreeTextBox修改字体格式等,又能保证安全? --------------------编程问答-------------------- 不一定,你客户端进行先编码也是可以的 --------------------编程问答-------------------- 你可以在提交的时候执行下面的代码
function myfunction() {
t = document.getElementById("TextBox1");
t.value = escape(t.value);
}
服务器端
Microsoft.JScript.GlobalObject.unescape(TextBox1.Text)
得到
例子
<%@ Page Language="C#" %>--------------------编程问答--------------------
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Button1_Click(object sender, EventArgs e)
{
String html = Microsoft.JScript.GlobalObject.unescape(TextBox1.Text);
Response.Write("你输入的是:" + Server.HtmlEncode(html)); //为了直接显示才进行HtmlEncode,实际不需要
TextBox1.Text = html;
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script>
function myfunction() {
t = document.getElementById("TextBox1");
t.value = escape(t.value);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:TextBox ID="TextBox1" runat="server" Height="275px" TextMode="MultiLine" Width="554px"></asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" OnClientClick="myfunction()" />
</form>
</body>
</html>
学习了。。。。
==================今天下午心情不好,就在CSDN混了。。========================= --------------------编程问答-------------------- protected void Page_Error(object sender, EventArgs e)
{
Exception ex = Server.GetLastError();
if (HttpContext.Current.Server.GetLastError() is HttpRequestValidationException)
{
HttpContext.Current.Response.Write("请输入合法的字符串【<a href=/"javascript:history.back(0);/">返回</a>】");
HttpContext.Current.Server.ClearError();
}
}
在你的提交事件里面写
//将输入字符串编码,这样所有的HTML标签都失效了。
StringBuilder sb = new StringBuilder(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());
补充:.NET技术 , ASP.NET