net過濾
❶ ASP.NET過濾html標簽的幾種常用方法
沒有幾種.
原理就是刪掉正則匹配到的html標簽
至於刪除用repalce還是remove我覺得沒那麼重要了就..
html標簽的正則,網上抄的不知道對不對:
"<(.[^>]*)>"
❷ c#.net正則表達式 過濾圖片,一次過濾字元串中的所有圖片,怎麼寫
你好!
是過濾掉HTML圖像標簽?下面的圖片是過濾後的常規方法如下:
海峽=
Regex.Replace(STR,「]
*>」,「」,RegexOptions.IgnoreCase);
打字不易,採納哦!
❸ 用C#在.net 留言板中加入臟話過濾功能
用strin.indexof();
例如:
string str = "我說罵人的話";
str.indexof("罵人的話");
返回所在字元的下標,如果沒找到返回-1.
if(str.indexof("罵人的話") == -1 )
{
發表留言!
}
else
{
不許罵人。。。
}
可以用這個方法過濾任何字元!也是最簡單的方法!正在表達式也能,有興趣可以去搜搜,但是比較難看懂!
那就用替換:
string str = "源字元串";
str = Regex.Replace(str, "(罵1|罵2|罵3|....)", "*", RegexOptions.IgnoreCase);
我給你寫了一遍,你看吧.界面有一個lab,一個textbox,一個按鈕
按鈕代碼如下:
private void button1_Click(object sender, EventArgs e)
{
string message = this.textBox1.Text.ToString();
message = Regex.Replace(message.Trim(),"(雞|鴨|日)", " *", RegexOptions.IgnoreCase);
this.label1.Text = message;
}
我試過了,絕度沒問題的.想過濾什麼字在那添加就哦了用"|"隔開!
❹ asp.net過濾過濾字元串,求正則表達式,style=「 任意css樣式 」過濾掉
/style="[^"]*"/
這是正則,把他鋪貨到的替換成空就好了。
❺ asp.net 過濾html代碼
默認來是禁止自包含有HTML標簽的POST請求,設置
ValidateRequest="false"
就可以了
比如:
<%@
Page
Language="C#"
AutoEventWireup="true"
CodeBehind="Default.aspx.cs"
Inherits=YourNameSpace.YourClassName"
ValidateRequest="false"
%>
❻ .net 客戶端數據過濾
用linq語句中的where,添加System.Linq引用回。答
List<Order> l = List<Order>();
List<Order> s = l.Where(w=>w.Status==1).ToList();
❼ C#NET裡面怎樣實現多重過濾選擇
可以用if判斷,foreach,while都可以啊,只要有好的結合起來就可以了,就是這樣的判斷有代碼有點麻煩,例如if就可以判斷很多,if裡面加if,else if else ,裡面還可以加多層if
❽ 有.net過濾字元的代碼么,詳細的,全的
實例講述了ASP.NET過濾HTML字元串的方法,供大家參考使用,具體代碼如下:
/// <summary>去除HTML標記
///
/// </summary>
/// <param name="Htmlstring">包括HTML的源碼</param>
/// <returns>已經去除後的文字</returns>
public static string GetNoHTMLString(string Htmlstring)
{
//刪除腳本
Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
//刪除HTML
Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", " ", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(|#169);", "\xa9", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"(\d+);", "", RegexOptions.IgnoreCase);
Htmlstring.Replace("<", "");
Htmlstring.Replace(">", "");
Htmlstring.Replace("\r\n", "");
Htmlstring = HttpContext.Current.Server.HtmlEncode(Htmlstring).Trim();
return Htmlstring;
}
/// <summary>獲取顯示的字元串,可顯示HTML標簽,但把危險的HTML標簽過濾,如iframe,script等。
///
/// </summary>
/// <param name="str">未處理的字元串</param>
/// <returns></returns>
public static string GetSafeHTMLString(string str)
{
str = Regex.Replace(str, @"<applet[^>]*?>.*?</applet>", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, @"<body[^>]*?>.*?</body>", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, @"<embed[^>]*?>.*?</embed>", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, @"<frame[^>]*?>.*?</frame>", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, @"<frameset[^>]*?>.*?</frameset>", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, @"<html[^>]*?>.*?</html>", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, @"<iframe[^>]*?>.*?</iframe>", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, @"<style[^>]*?>.*?</style>", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, @"<layer[^>]*?>.*?</layer>", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, @"<link[^>]*?>.*?</link>", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, @"<ilayer[^>]*?>.*?</ilayer>", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, @"<meta[^>]*?>.*?</meta>", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, @"<object[^>]*?>.*?</object>", "", RegexOptions.IgnoreCase);
return str;
}
❾ asp.net 插入HTML字元串怎麼避免被過濾
Asp.net中如何過濾,js,css代碼
以下為引用的內容:
#region/// 過濾html,js,css代碼
/// <summary>
/// 過濾html,js,css代碼
/// </summary>
/// <param name="html">參數傳入</param>
/// <returns></returns>
public static string CheckStr(string html)
{
System.Text.RegularExpressions.Regex regex1 = new System.Text.RegularExpressions.Regex(@"<script[\s\S]+</script *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex2 = new System.Text.RegularExpressions.Regex(@" href *= *[\s\S]*script *:", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex3 = new System.Text.RegularExpressions.Regex(@" no[\s\S]*=", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex4 = new System.Text.RegularExpressions.Regex(@"<iframe[\s\S]+</iframe *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex5 = new System.Text.RegularExpressions.Regex(@"<frameset[\s\S]+</frameset *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex6 = new System.Text.RegularExpressions.Regex(@"\<img[^\>]+\>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex7 = new System.Text.RegularExpressions.Regex(@"</p>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex8 = new System.Text.RegularExpressions.Regex(@"<p>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex9 = new System.Text.RegularExpressions.Regex(@"<[^>]*>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
html = regex1.Replace(html, ""); //過濾<script></script>標記
html = regex2.Replace(html, ""); //過濾href=javascript: (<A>) 屬性
html = regex3.Replace(html, " _disibledevent="); //過濾其它控制項的on...事件
html = regex4.Replace(html, ""); //過濾iframe
html = regex5.Replace(html, ""); //過濾frameset
html = regex6.Replace(html, ""); //過濾frameset
html = regex7.Replace(html, ""); //過濾frameset
html = regex8.Replace(html, ""); //過濾frameset
html = regex9.Replace(html, "");
html = html.Replace(" ", "");
html = html.Replace("</strong>", "");
html = html.Replace("<strong>", "");
return html;
}
#endregion
#region /// 過濾p /p代碼
/// <summary>
/// 過濾p /p代碼
/// </summary>
/// <param name="html">參數傳入</param>
/// <returns></returns>
public static string InputStr(string html)
{
html = html.Replace(@"\<img[^\>]+\>", "");
html = html.Replace(@"<p>", "");
html = html.Replace(@"</p>", "");
return html;
}
#endregion
❿ 在asp.net傳遞參數怎麼過濾特殊字元
1.傳遞前先加密 ,KEY是你自己定義的,加密解密函數KEY一致。
public static string Encode(string str, string key)
{
DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
provider.Key = Encoding.ASCII.GetBytes(key.Substring(0, 8));
provider.IV = Encoding.ASCII.GetBytes(key.Substring(0, 8));
byte[] bytes = Encoding.GetEncoding("GB2312").GetBytes(str);
MemoryStream stream = new MemoryStream();
CryptoStream stream2 = new CryptoStream(stream, provider.CreateEncryptor(), CryptoStreamMode.Write);
stream2.Write(bytes, 0, bytes.Length);
stream2.FlushFinalBlock();
StringBuilder builder = new StringBuilder();
foreach (byte num in stream.ToArray())
{
builder.AppendFormat("{0:X2}", num);
}
stream.Close();
return builder.ToString();
}
2.獲取參數後,解密
/// <summary>
/// Des 解密 GB2312
/// </summary>
/// <param name="str">Desc string</param>
/// <param name="key">Key ,必須為8位 </param>
/// <returns></returns>
public static string Decode(string str, string key)
{
try
{
DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
provider.Key = Encoding.ASCII.GetBytes(key.Substring(0, 8));
provider.IV = Encoding.ASCII.GetBytes(key.Substring(0, 8));
byte[] buffer = new byte[str.Length / 2];
for (int i = 0; i < (str.Length / 2); i++)
{
int num2 = Convert.ToInt32(str.Substring(i * 2, 2), 0x10);
buffer[i] = (byte)num2;
}
MemoryStream stream = new MemoryStream();
CryptoStream stream2 = new CryptoStream(stream, provider.CreateDecryptor(), CryptoStreamMode.Write);
stream2.Write(buffer, 0, buffer.Length);
stream2.FlushFinalBlock();
stream.Close();
return Encoding.GetEncoding("GB2312").GetString(stream.ToArray());
}
catch (Exception)
{
return "";
}
}