日期:2014-05-17 浏览次数:21392 次
使用HTML模板发送邮件,邮件中含有一个链接,点击链接打开浏览器,完成相应功能。
链接使用http://www.******.com?id=123456&name=Nicholas这种URL后面加连接字符串的形式。
问号后面的字符串是明文,不能传输一些敏感数据,并且容易遭到篡改。
将问号后面的字符串加密,需要的时候再解密。
使用html模板发送电子邮件在上篇博文《asp.net使用html模板发送电子邮件》已有介绍。
public static class Decryption { private static byte[] key = { 0x21, 0x43, 0x65, 0x87, 0x09, 0xBA, 0xDC, 0xFE }; private static byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF }; /// <summary> /// 加密处理 /// </summary> /// <param name="Input"></param> /// <returns></returns> public static string Encrypt(string Input) { try { DESCryptoServiceProvider des = new DESCryptoServiceProvider(); Byte[] inputByteArray = Encoding.UTF8.GetBytes(Input); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(key, IV), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); return Convert.ToBase64String(ms.ToArray()); } catch (Exception ex) { return ""; } } /// <summary> /// 解密处理 /// </summary> /// <param name="Input"></param> /// <returns></returns> public static string Decrypt(string Input) { if (!string.IsNullOrEmpty(Input)) { Input = Input.Replace(" ", "+"); Byte[] inputByteArray = new Byte[Input.Length]; try { DESCryptoServiceProvider des = new DESCryptoServiceProvider(); inputByteArray = Convert.FromBase64String(Input); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(key, IV), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); Encoding encoding = Encoding.UTF8; return encoding.GetString(ms.ToArray()); } catch (Exception ex) { return ""; } } else { return ""; } } }
/// <summary> /// 替换email中的字符串 /// </summary> /// <returns></returns> public static String getEmailStr(string url,string userName, string s_key) { //s_key为随机生成的字符串 String str = userName+ "," +s_key; return "<a href=\"" + url + "?" + Decryption.Encrypt(str) + "\"></a>"; }
try { param = Request.Url.Query; param = param.Substring(1, param.