⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 datasecurity.cs

📁 动易SiteFactory&#8482 网上商店系统1.0源代码
💻 CS
字号:
namespace PowerEasy.Common
{
    using System;
    using System.Collections.ObjectModel;
    using System.Security.Cryptography;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Web;

    public abstract class DataSecurity
    {
        protected DataSecurity()
        {
        }

        public static string ConvertToJavaScript(string str)
        {
            str = str.Replace(@"\", @"\\");
            str = str.Replace("\n", @"\n");
            str = str.Replace("\r", @"\r");
            str = str.Replace("\"", "\\\"");
            return str;
        }

        public static string FilterBadChar(string strchar)
        {
            string str2 = "";
            if (string.IsNullOrEmpty(strchar))
            {
                return "";
            }
            string str = strchar;
            string[] strArray = new string[] { 
                "+", "'", "--", "%", "^", "&", "?", "(", ")", "<", ">", "[", "]", "{", "}", "/", 
                "\"", ";", ":", "Chr(34)", "Chr(0)"
             };
            StringBuilder builder = new StringBuilder(str);
            for (int i = 0; i < strArray.Length; i++)
            {
                str2 = builder.Replace(strArray[i], "").ToString();
            }
            return builder.Replace("@@", "@").ToString();
        }

        public static string GetArrayValue(int index, string[] field)
        {
            if ((field != null) && ((index >= 0) && (index < field.Length)))
            {
                return field[index];
            }
            return string.Empty;
        }

        public static string GetArrayValue(int index, Collection<string> field)
        {
            if ((index >= 0) && (index < field.Count))
            {
                return field[index];
            }
            return string.Empty;
        }

        public static string HtmlDecode(object o)
        {
            if (o == null)
            {
                return null;
            }
            return HtmlDecode(o.ToString());
        }

        public static string HtmlDecode(string str)
        {
            if (!string.IsNullOrEmpty(str))
            {
                str = str.Replace("<br>", "\n");
                str = str.Replace("&gt;", ">");
                str = str.Replace("&lt;", "<");
                str = str.Replace("&nbsp;", " ");
                str = str.Replace("&#39;", "'");
                str = str.Replace("&quot;", "\"");
            }
            return str;
        }

        public static string HtmlEncode(object o)
        {
            if (o == null)
            {
                return null;
            }
            return HtmlEncode(o.ToString());
        }

        public static string HtmlEncode(string str)
        {
            if (!string.IsNullOrEmpty(str))
            {
                str = str.Replace("<", "&lt;");
                str = str.Replace(">", "&gt;");
                str = str.Replace(" ", "&nbsp;");
                str = str.Replace("'", "&#39;");
                str = str.Replace("\"", "&quot;");
                str = str.Replace("\r\n", "<br>");
                str = str.Replace("\n", "<br>");
            }
            return str;
        }

        public static int Len(string str)
        {
            int num = 0;
            foreach (char ch in str)
            {
                if (ch > '\x007f')
                {
                    num += 2;
                }
                else
                {
                    num++;
                }
            }
            return num;
        }

        public static string MakeFileRndName()
        {
            return (DateTime.Now.ToString("yyyyMMddHHmmss") + MakeRandomString("0123456789", 4));
        }

        public static string MakeFolderName()
        {
            return DateTime.Now.ToString("yyyyMM");
        }

        public static string MakeRandomString(int pwdlen)
        {
            return MakeRandomString("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_*", pwdlen);
        }

        public static string MakeRandomString(string pwdchars, int pwdlen)
        {
            StringBuilder builder = new StringBuilder();
            Random random = new Random();
            for (int i = 0; i < pwdlen; i++)
            {
                int num = random.Next(pwdchars.Length);
                builder.Append(pwdchars[num]);
            }
            return builder.ToString();
        }

        public static string RandomNum()
        {
            return RandomNum(4);
        }

        public static string RandomNum(int intlong)
        {
            Random random = new Random();
            StringBuilder builder = new StringBuilder("");
            for (int i = 0; i < intlong; i++)
            {
                builder.Append(random.Next(10));
            }
            return builder.ToString();
        }

        public static string RestrictedUrl(Uri url)
        {
            Uri uri;
            if (url == null)
            {
                return null;
            }
            Uri.TryCreate(url.AbsolutePath, UriKind.Absolute, out uri);
            return (RestrictedUrl(uri) + url.Query);
        }

        public static string RngCspNum(int strLength)
        {
            if (strLength > 0)
            {
                strLength--;
            }
            else
            {
                strLength = 5;
            }
            byte[] data = new byte[strLength];
            new RNGCryptoServiceProvider().GetBytes(data);
            return BitConverter.ToInt32(data, 0).ToString();
        }

        public static string Strings(string ichar, int i)
        {
            StringBuilder builder = new StringBuilder("");
            for (int j = 0; j < i; j++)
            {
                builder.Append(ichar);
            }
            return builder.ToString();
        }

        public static string UnrestrictedUrl(string path)
        {
            if (string.IsNullOrEmpty(path))
            {
                return path;
            }
            if (VirtualPathUtility.IsAppRelative(path))
            {
                path = VirtualPathUtility.ToAbsolute(path);
            }
            int num = 80;
            string host = HttpContext.Current.Request.Url.Host;
            string str2 = (num != 80) ? string.Format(":{0}", num) : "";
            Uri baseUri = new Uri(string.Format("http://{0}{1}", host, str2));
            return new Uri(baseUri, path).ToString();
        }

        public static string UrlEncode(object urlObj)
        {
            if (urlObj == null)
            {
                return null;
            }
            return UrlEncode(urlObj.ToString());
        }

        public static string UrlEncode(string urlStr)
        {
            if (string.IsNullOrEmpty(urlStr))
            {
                return null;
            }
            return Regex.Replace(urlStr, @"[^a-zA-Z0-9,-_\.]+", new MatchEvaluator(DataSecurity.UrlEncodeMatch));
        }

        private static string UrlEncodeMatch(Match match)
        {
            string str = match.ToString();
            if (str.Length < 1)
            {
                return str;
            }
            StringBuilder builder = new StringBuilder();
            foreach (char ch in str)
            {
                if (ch > '\x007f')
                {
                    builder.AppendFormat("%u{0:X4}", (int) ch);
                }
                else
                {
                    builder.AppendFormat("%{0:X2}", (int) ch);
                }
            }
            return builder.ToString();
        }

        public static string XmlEncode(string str)
        {
            if (!string.IsNullOrEmpty(str))
            {
                str = str.Replace("&", "&amp;");
                str = str.Replace("<", "&lt;");
                str = str.Replace(">", "&gt;");
                str = str.Replace("'", "&apos;");
                str = str.Replace("\"", "&quot;");
            }
            return str;
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -