• 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);//使之汉字不出现乱码







    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(" ", "");//避免出现<br>等标签后被认为加上 的换行符,这会出现在多行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;
        }

  • 相关阅读:
    NumberFormat注解 DateTimeFormat
    SpringMVC自定义视图
    SpringMVC访问静态资源
    Jmeter查看结果树乱码的情况解决
    JMETER设置默认启动为中文的方法
    python——for循环与while循环的区别
    python学习笔记——字符串也可加入到for循环中
    python学习笔记——列表与字典的区别
    python学习笔记——删除列表元素的三种方法
    python学习笔记——input函数的使用规则
  • 原文地址:https://www.cnblogs.com/flysnow-z/p/4683035.html
Copyright © 2020-2023  润新知