常用函数:
CommonHelper.cs
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Security.Cryptography; using System.Text; using System.Threading.Tasks; using System.Web; using RazorEngine; using RazorEngine.Text; namespace DIDAO.Common { public class CommonHelper { /// <summary> /// 传入虚拟路径 返回全路径的html字符串 /// </summary> /// <param name="context">当前数据上下文</param> /// <param name="virtualPath">虚拟路径</param> /// <returns>返回全路径的html字符串</returns> public static string GetHtmlFromVirtualPath(HttpContext context,string virtualPath) { string fileFullPath = context.Server.MapPath(virtualPath); string html = File.ReadAllText(fileFullPath); return html; } /// <summary> /// 输出错误信息到错误界面 /// </summary> /// <param name="context">当前数据上下文</param> /// <param name="virtualPath">错误页面的 虚拟路径</param> /// <param name="errormsg">错误信息</param> public static void OutputError(HttpContext context, string virtualPath,string errormsg) { string htmlError = GetHtmlFromVirtualPath(context, virtualPath); string htmlNewError = htmlError.Replace("{errormsg}", errormsg); context.Response.Write(htmlNewError); } /// <summary> /// 加密 /// </summary> /// <param name="str">原始密码</param> /// <returns>加密密码</returns> public static string Md5Encode(string str) { byte[] bytes = System.Text.Encoding.Default.GetBytes(str); MD5 md5 = new MD5CryptoServiceProvider(); byte[] newBytes = md5.ComputeHash(bytes); string newStr = BitConverter.ToString(newBytes).Replace("-",""); return newStr; } /// <summary> /// 检查字符串 是否含有特殊字符 :含有-false /// </summary> /// <param name="str">输入字符串</param> /// <returns>是否含有特殊字符:含有-false</returns> public static bool CheckStringIsSpecialChar(string str) { bool flag = true; char[] chs = { ',', '/', '\\', '\'', '"' }; foreach (char ch in chs) { if (str.IndexOf(ch) >= 0) { flag = false; } } return flag; } //DES NET加密解密 public static string _KEY = "YANGGUO1"; //密钥 public static string _IV = "YANGGUO2"; //向量 /// <summary> /// 加密 /// </summary> /// <param name="data"></param> /// <returns></returns> public static string DesEncode(string data) { byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(_KEY); byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(_IV); DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider(); int i = cryptoProvider.KeySize; MemoryStream ms = new MemoryStream(); CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateEncryptor(byKey, byIV), CryptoStreamMode.Write); StreamWriter sw = new StreamWriter(cst); sw.Write(data); sw.Flush(); cst.FlushFinalBlock(); sw.Flush(); string strRet = Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length); return strRet; } /// <summary> /// 解密 /// </summary> /// <param name="data"></param> /// <returns></returns> public static string DesDecode(string data) { byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(_KEY); byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(_IV); byte[] byEnc; try { data.Replace("_%_", "/"); data.Replace("-%-", "#"); byEnc = Convert.FromBase64String(data); } catch { return null; } DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider(); MemoryStream ms = new MemoryStream(byEnc); CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateDecryptor(byKey, byIV), CryptoStreamMode.Read); StreamReader sr = new StreamReader(cst); return sr.ReadToEnd(); } } }