1.输入框过滤或替换恶意字符
/// <summary>
/// Method to make sure that user's inputs are not malicious
/// </summary>
/// <param name="text">User's Input</param>
/// <param name="maxLength">Maximum length of input</param>
/// <returns>The cleaned up version of the input</returns>
public static string InputText(string text, int maxLength) {
text = text.Trim();
if (string.IsNullOrEmpty(text))
return string.Empty;
if (text.Length > maxLength)
text = text.Substring(0, maxLength);
text = Regex.Replace(text, "[\\s]{2,}", " "); //two or more spaces
text = Regex.Replace(text, "(<[b|B][r|R]/*>)+|(<[p|P](.|\\n)*?>)", "\n"); //<br>
text = Regex.Replace(text, "(\\s*&[n|N][b|B][s|S][p|P];\\s*)+", " "); //
text = Regex.Replace(text, "<(.|\\n)*?>", string.Empty); //any other tags
text = text.Replace("'", "''");
return text;
}
/// <summary>
/// Method to make sure that user's inputs are not malicious
/// </summary>
/// <param name="text">User's Input</param>
/// <param name="maxLength">Maximum length of input</param>
/// <returns>The cleaned up version of the input</returns>
public static string InputText(string text, int maxLength) {
text = text.Trim();
if (string.IsNullOrEmpty(text))
return string.Empty;
if (text.Length > maxLength)
text = text.Substring(0, maxLength);
text = Regex.Replace(text, "[\\s]{2,}", " "); //two or more spaces
text = Regex.Replace(text, "(<[b|B][r|R]/*>)+|(<[p|P](.|\\n)*?>)", "\n"); //<br>
text = Regex.Replace(text, "(\\s*&[n|N][b|B][s|S][p|P];\\s*)+", " "); //
text = Regex.Replace(text, "<(.|\\n)*?>", string.Empty); //any other tags
text = text.Replace("'", "''");
return text;
}
2. 缓存设置,缓存时间和是否开启缓存配置到配置文件中方便我们关闭和启用缓存
private static readonly int productTimeout = int.Parse(ConfigurationManager.AppSettings["ProductCacheDuration"]);
private static readonly bool enableCaching = bool.Parse(ConfigurationManager.AppSettings["EnableCaching"]);
/// <summary>
/// This method acts as a proxy between the web and business components to check whether the
/// underlying data has already been cached.
/// </summary>
/// <param name="category">Category</param>
/// <returns>List of ProductInfo from Cache or Business component</returns>
public static IList<ProductInfo> GetProductsByCategory(string category) {
Product product = new Product();
if (!enableCaching)
return product.GetProductsByCategory(category);
string key = "product_by_category_" + category;
IList<ProductInfo> data = (IList<ProductInfo>)HttpRuntime.Cache[key];
// Check if the data exists in the data cache
if (data == null) {
// If the data is not in the cache then fetch the data from the business logic tier
data = product.GetProductsByCategory(category);
// Create a AggregateCacheDependency object from the factory
AggregateCacheDependency cd = DependencyFacade.GetProductDependency();
// Store the output in the data cache, and Add the necessary AggregateCacheDependency object
HttpRuntime.Cache.Add(key, data, cd, DateTime.Now.AddHours(productTimeout), Cache.NoSlidingExpiration, CacheItemPriority.High, null);
}
return data;
}
private static readonly int productTimeout = int.Parse(ConfigurationManager.AppSettings["ProductCacheDuration"]);
private static readonly bool enableCaching = bool.Parse(ConfigurationManager.AppSettings["EnableCaching"]);
/// <summary>
/// This method acts as a proxy between the web and business components to check whether the
/// underlying data has already been cached.
/// </summary>
/// <param name="category">Category</param>
/// <returns>List of ProductInfo from Cache or Business component</returns>
public static IList<ProductInfo> GetProductsByCategory(string category) {
Product product = new Product();
if (!enableCaching)
return product.GetProductsByCategory(category);
string key = "product_by_category_" + category;
IList<ProductInfo> data = (IList<ProductInfo>)HttpRuntime.Cache[key];
// Check if the data exists in the data cache
if (data == null) {
// If the data is not in the cache then fetch the data from the business logic tier
data = product.GetProductsByCategory(category);
// Create a AggregateCacheDependency object from the factory
AggregateCacheDependency cd = DependencyFacade.GetProductDependency();
// Store the output in the data cache, and Add the necessary AggregateCacheDependency object
HttpRuntime.Cache.Add(key, data, cd, DateTime.Now.AddHours(productTimeout), Cache.NoSlidingExpiration, CacheItemPriority.High, null);
}
return data;
}
Js,Firmly put your fade