AntiXSS v4.0中Sanitizer.GetSafeHtmlFragment等方法将部分汉字编码为乱码的解决方案

时间:2023-02-06 09:22:41

AntiXSS v4.0中Sanitizer.GetSafeHtmlFragment等方法将部分汉字编码为乱码的解决方案

以下代码为asp.net环境下,c#语言编写的解决方案.
数据用Dictionary,而不是用其他的例如ArrayList存放,是为了速度.
将Dictionary替换为HashTable也是一个不错的解决方案
调用举例

String abc = //....你的某些赋值行为,这里的字符串是要处理的内容
.............
abc=Sanitizer.GetSafeHtmlFragment(abc);//被AntiXSS处理
abc=hfjString_SanitizerCompatibleWithChineseCharacters(abc);//使之汉字不出现乱码


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("丼", "丼");
                        hbjDictionaryFX.Add("似", "似");
                        hbjDictionaryFX.Add("值", "值");
                        hbjDictionaryFX.Add("儼", "儼");
                        hbjDictionaryFX.Add("刼", "刼");
                        hbjDictionaryFX.Add("匼", "匼");
                        hbjDictionaryFX.Add("吼", "吼");
                        hbjDictionaryFX.Add("唼", "唼");
                        hbjDictionaryFX.Add("嘼", "嘼");
                        hbjDictionaryFX.Add("圼", "圼");
                        hbjDictionaryFX.Add("堼", "堼");
                        hbjDictionaryFX.Add("尼", "尼");
                        hbjDictionaryFX.Add("格", "格");
                        hbjDictionaryFX.Add("夼", "夼");
                        hbjDictionaryFX.Add("娼", "娼");
                        hbjDictionaryFX.Add("嬼", "嬼");
                        hbjDictionaryFX.Add("崼", "崼");
                        hbjDictionaryFX.Add("帼", "帼");
                        hbjDictionaryFX.Add("弼", "弼");
                        hbjDictionaryFX.Add("怼", "怼");
                        hbjDictionaryFX.Add("愼", "愼");
                        hbjDictionaryFX.Add("戼", "戼");
                        hbjDictionaryFX.Add("挼", "挼");
                        hbjDictionaryFX.Add("搼", "搼");
                        hbjDictionaryFX.Add("攼", "攼");
                        hbjDictionaryFX.Add("昼", "昼");
                        hbjDictionaryFX.Add("朼", "朼");
                        hbjDictionaryFX.Add("椼", "椼");
                        hbjDictionaryFX.Add("樼", "樼");
                        hbjDictionaryFX.Add("欼", "欼");
                        hbjDictionaryFX.Add("氼", "氼");
                        hbjDictionaryFX.Add("洼", "洼");
                        hbjDictionaryFX.Add("渼", "渼");
                        hbjDictionaryFX.Add("漼", "漼");
                        hbjDictionaryFX.Add("瀼", "瀼");
                        hbjDictionaryFX.Add("焼", "焼");
                        hbjDictionaryFX.Add("爼", "爼");
                        hbjDictionaryFX.Add("猼", "猼");
                        hbjDictionaryFX.Add("琼", "琼");
                        hbjDictionaryFX.Add("甼", "甼");
                        hbjDictionaryFX.Add("瘼", "瘼");
                        hbjDictionaryFX.Add("眼", "眼");
                        hbjDictionaryFX.Add("砼", "砼");
                        hbjDictionaryFX.Add("礼", "礼");
                        hbjDictionaryFX.Add("稼", "稼");
                        hbjDictionaryFX.Add("笼", "笼");
                        hbjDictionaryFX.Add("簼", "簼");
                        hbjDictionaryFX.Add("紼", "紼");
                        hbjDictionaryFX.Add("縼", "縼");
                        hbjDictionaryFX.Add("缼", "缼");
                        hbjDictionaryFX.Add("耼", "耼");
                        hbjDictionaryFX.Add("脼", "脼");
                        hbjDictionaryFX.Add("舼", "舼");
                        hbjDictionaryFX.Add("茼", "茼");
                        hbjDictionaryFX.Add("萼", "萼");
                        hbjDictionaryFX.Add("蔼", "蔼");
                        hbjDictionaryFX.Add("贼", "贼");
                        hbjDictionaryFX.Add("鬼", "鬼");
                    }
                }

}

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

}

return hbString_ReturnValue;
    }