码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2015-07-28 17:41:22      阅读:965      评论:0      收藏:0      [点我收藏+]

标签:

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("&#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;
    }

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

标签:

原文地址:http://www.cnblogs.com/flysnow-z/p/4683035.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!