/// <summary> /// /// </summary> /// <param name="ipList"></param> /// <returns></returns> public static string EncodeLockIP(string ipList) { StringBuilder builder = new StringBuilder(0x100); if (!string.IsNullOrEmpty(ipList.Trim())) { string[] strArray = ipList.Split(new char[] { '\n' }); for (int i = 0; i < strArray.Length; i++) { if (!string.IsNullOrEmpty(strArray[i]) && strArray[i].Contains("----")) { string[] strArray2 = strArray[i].Split(new string[] { "----" }, StringSplitOptions.RemoveEmptyEntries); if (strArray2.Length < 2) { throw new ArgumentException("请填写正确网站黑白名单中的IP地址!"); } if (!JosonValidate.IsIP(strArray2[0]) || !JosonValidate.IsIP(strArray2[1])) { throw new ArgumentException("请填写正确网站黑白名单中的IP地址!"); } if (i == 0) { builder.Append(EncodeIP(strArray2[0]) + "----" + EncodeIP(strArray2[1])); } else { builder.Append(string.Concat(new object[] { "$$$", EncodeIP(strArray2[0]), "----", EncodeIP(strArray2[1]) })); } } } } return(builder.ToString()); }
/// <summary> /// 获得指定Url参数的值 /// </summary> /// <param name="strName">Url参数</param> /// <param name="sqlSafeCheck">是否进行SQL安全检查</param> /// <returns>Url参数的值</returns> public static string GetQueryString(string strName, bool sqlSafeCheck = false) { if (HttpContext.Current.Request.QueryString[strName] == null) { return(""); } if (sqlSafeCheck && !JosonValidate.IsSafeSqlString(HttpContext.Current.Request.QueryString[strName])) { return("unsafe string"); } return(HttpContext.Current.Request.QueryString[strName]); }
/// <summary> /// 获取客户端IP /// </summary> /// <returns></returns> public static string GetIP() { string userHostAddress = string.Empty; userHostAddress = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; if (string.IsNullOrEmpty(userHostAddress)) { userHostAddress = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]; } if (string.IsNullOrEmpty(userHostAddress)) { userHostAddress = HttpContext.Current.Request.UserHostAddress; } if (!(!string.IsNullOrEmpty(userHostAddress) && JosonValidate.IsIP(userHostAddress))) { return("127.0.0.1"); } return(userHostAddress); }
/// <summary> /// GetQuery /// </summary> /// <param name="strName"></param> /// <returns></returns> public static string[] GetQuerys(string strName) { string ids = GetString(strName); List <string> result = new List <string>(); var array = ids.Split(','); foreach (var a in array) { if (JosonValidate.NotIsNullOrEmpty(a)) { result.Add(a); } else { result.Add("Null"); } } return(result.ToArray()); }
/// <summary> /// List转成json /// </summary> /// <typeparam name="T"></typeparam> /// <param name="jsonName"></param> /// <param name="list"></param> /// <returns></returns> public static string ToJson <T>(this IList <T> list, string jsonName) { StringBuilder Json = new StringBuilder(); if (string.IsNullOrEmpty(jsonName)) { if (list.Count > 1) { jsonName = list[0].GetType().Name; } else { jsonName = list.GetType().FullName; } } Json.Append("{\"" + jsonName + "\":["); if (list.Count > 0) { for (int i = 0; i < list.Count; i++) { T obj = Activator.CreateInstance <T>(); PropertyInfo[] pi = obj.GetType().GetProperties(); Json.Append("{"); for (int j = 0; j < pi.Length; j++) { Type type = pi[j].PropertyType; String strValue = string.Empty; #region 查看其是否Attributes bool IsIgnoreAttribute = false; #region 取出所以属性时 需要遍历 /* * // 取出所以属性 * object[] objAttribute = pi[j].GetCustomAttributes(typeof(Attribute), true); * * for (int attrU = 0; attrU < objAttribute.Length; attrU++) * { * ScriptIgnoreAttribute JosonAttribute = objAttribute[attrU] as ScriptIgnoreAttribute; * * if (JosonAttribute != null) * { * IsIgnoreAttribute = JosonValidate.NotIsNullOrEmpty(JosonAttribute.GetType().Name.ToStrings()); * } * * } */ #endregion object[] objAttributes = pi[j].GetCustomAttributes(typeof(ScriptIgnoreAttribute), true); if (objAttributes.Length > 0) { ScriptIgnoreAttribute JosonAttribute = objAttributes[0] as ScriptIgnoreAttribute; if (JosonAttribute != null) { IsIgnoreAttribute = JosonValidate.NotIsNullOrEmpty(JosonAttribute.GetType().Name.ToStrings()); } } #endregion // 忽略有IgnoreAttribute 的属性 if (!IsIgnoreAttribute) { #region 反射获取属性及值 if (type.IsGenericType) { //是否泛型 #region 泛型Nullable<> Type genericTypeDefinition = pi[j].PropertyType.GetGenericTypeDefinition(); if (genericTypeDefinition == typeof(Nullable <>)) { //String value = Convert.ToString(pi[j].GetValue(list[i], null)); //pi[j].SetValue(List[0], string.IsNullOrEmpty(value) ? null : Convert.ChangeType(value, Nullable.GetUnderlyingType(type)), null); object o = pi[j].GetValue(list[i], null); type = Nullable.GetUnderlyingType(type); strValue = Convert.ToString(o); } #endregion } else { //String value = Convert.ToString(pi[j].GetValue(list[i], null)); //pi[j].SetValue(Lists, string.IsNullOrEmpty(value) ? null : Convert.ChangeType(value, Nullable.GetUnderlyingType(type)), null); type = pi[j].GetValue(list[i], null).ToStrings().GetType(); strValue = pi[j].GetValue(list[i], null).ToStrings(); } #endregion Json.Append("\"" + pi[j].Name.ToString() + "\":" + StringFormat(strValue, type)); if (j < pi.Length - 1) { Json.Append(","); } } } Json.Append("}"); if (i < list.Count - 1) { Json.Append(","); } } } Json.Append("]}"); return(Json.ToString()); }