AntiXSS v4.0中Sanitizer.GetSafeHtmlFragment等方法將部分漢字編碼爲亂碼的解決方案

AntiXSS v4.0中Sanitizer.GetSafeHtmlFragment等方法將部分漢字編碼爲亂碼的解決方案

如下代碼爲asp.net環境下,c#語言編寫的解決方案.
數據用Dictionary,而不是用其餘的例如ArrayList存放,是爲了速度.
將Dictionary替換爲HashTable也是一個不錯的解決方案
調用舉例

String abc = //....你的某些賦值行爲,這裏的字符串是要處理的內容
.............
abc=Sanitizer.GetSafeHtmlFragment(abc);//被AntiXSS處理
abc=hfjString_SanitizerCompatibleWithChineseCharacters(abc);//使之漢字不出現亂碼c#







private static System.Collections.Generic.Dictionary<string, string> hbjDictionaryFX = new System.Collections.Generic.Dictionary<string, string>();
    object hbObject鎖 = new object();
    /// <summary>
    /// 微軟的AntiXSS v4.0 讓部分漢字亂碼,這裏將亂碼部分漢字轉換回來
    /// </summary>
    /// <param name="hc輸入值"></param>
    /// <returns></returns>
    public static String hfjString_SanitizerCompatibleWithChineseCharacters(String hcString_Para)
    {
        string hbString_ReturnValue = hcString_Para;

        hbString_ReturnValue = hbString_ReturnValue.Replace("\r\n", "");//避免出現<br>等標籤後被認爲加上\r\n的換行符,這會出如今多行textbox控件中,不須要的人請註釋這一行代碼
        if (hbString_ReturnValue.Contains("&#"))
        {
            //Dictionary若是沒有內容就初始化內容
            if (hbjDictionaryFX.Keys.Count == 0)
            {
                lock (hbjDictionaryFX)
                {
                    if (hbjDictionaryFX.Keys.Count == 0)
                    {
                        hbjDictionaryFX.Clear();//防止多線程狀況下的不安全狀況,雙重檢查理論很完美,可是在多處理器,多線程下,會有平臺漏洞,緣由是亂序寫入這一cpu或系統功能的存在

                        hbjDictionaryFX.Add("&#20028;", "丼");
                        hbjDictionaryFX.Add("&#20284;", "似");
                        hbjDictionaryFX.Add("&#20540;", "值");
                        hbjDictionaryFX.Add("&#20796;", "儼");
                        hbjDictionaryFX.Add("&#21052;", "刼");
                        hbjDictionaryFX.Add("&#21308;", "匼");
                        hbjDictionaryFX.Add("&#21564;", "吼");
                        hbjDictionaryFX.Add("&#21820;", "唼");
                        hbjDictionaryFX.Add("&#22076;", "嘼");
                        hbjDictionaryFX.Add("&#22332;", "圼");
                        hbjDictionaryFX.Add("&#22588;", "堼");
                        hbjDictionaryFX.Add("&#23612;", "尼");
                        hbjDictionaryFX.Add("&#26684;", "格");
                        hbjDictionaryFX.Add("&#22844;", "夼");
                        hbjDictionaryFX.Add("&#23100;", "娼");
                        hbjDictionaryFX.Add("&#23356;", "嬼");
                        hbjDictionaryFX.Add("&#23868;", "崼");
                        hbjDictionaryFX.Add("&#24124;", "幗");
                        hbjDictionaryFX.Add("&#24380;", "弼");
                        hbjDictionaryFX.Add("&#24636;", "懟");
                        hbjDictionaryFX.Add("&#24892;", "愼");
                        hbjDictionaryFX.Add("&#25148;", "戼");
                        hbjDictionaryFX.Add("&#25404;", "挼");
                        hbjDictionaryFX.Add("&#25660;", "搼");
                        hbjDictionaryFX.Add("&#25916;", "攼");
                        hbjDictionaryFX.Add("&#26172;", "晝");
                        hbjDictionaryFX.Add("&#26428;", "朼");
                        hbjDictionaryFX.Add("&#26940;", "椼");
                        hbjDictionaryFX.Add("&#27196;", "樼");
                        hbjDictionaryFX.Add("&#27452;", "欼");
                        hbjDictionaryFX.Add("&#27708;", "氼");
                        hbjDictionaryFX.Add("&#27964;", "窪");
                        hbjDictionaryFX.Add("&#28220;", "渼");
                        hbjDictionaryFX.Add("&#28476;", "漼");
                        hbjDictionaryFX.Add("&#28732;", "瀼");
                        hbjDictionaryFX.Add("&#28988;", "焼");
                        hbjDictionaryFX.Add("&#29244;", "爼");
                        hbjDictionaryFX.Add("&#29500;", "猼");
                        hbjDictionaryFX.Add("&#29756;", "瓊");
                        hbjDictionaryFX.Add("&#30012;", "甼");
                        hbjDictionaryFX.Add("&#30268;", "瘼");
                        hbjDictionaryFX.Add("&#30524;", "眼");
                        hbjDictionaryFX.Add("&#30780;", "砼");
                        hbjDictionaryFX.Add("&#31036;", "禮");
                        hbjDictionaryFX.Add("&#31292;", "稼");
                        hbjDictionaryFX.Add("&#31548;", "籠");
                        hbjDictionaryFX.Add("&#31804;", "簼");
                        hbjDictionaryFX.Add("&#32060;", "紼");
                        hbjDictionaryFX.Add("&#32316;", "縼");
                        hbjDictionaryFX.Add("&#32572;", "缼");
                        hbjDictionaryFX.Add("&#32828;", "耼");
                        hbjDictionaryFX.Add("&#33084;", "脼");
                        hbjDictionaryFX.Add("&#33340;", "舼");
                        hbjDictionaryFX.Add("&#33596;", "茼");
                        hbjDictionaryFX.Add("&#33852;", "萼");
                        hbjDictionaryFX.Add("&#34108;", "藹");
                        hbjDictionaryFX.Add("&#36156;", "賊");
                        hbjDictionaryFX.Add("&#39740;", "鬼");
                    }
                }

            }

            //開始替換的遍歷
            foreach (string key in hbjDictionaryFX.Keys)
            {
                if (hbString_ReturnValue.Contains(key))
                {
                    hbString_ReturnValue = hbString_ReturnValue.Replace(key, hbjDictionaryFX[key]);
                }
            }

        }

        return hbString_ReturnValue;
    }

安全

相關文章
相關標籤/搜索