加密Cookies和解密Cookies方法: 加密/解密方法 DESEncrypt public class DESEncrypt { #region ========加密======== /// <summary> /// 加密 /// </summary> /// <param name="Text"></param> /// <returns></returns> public static string Encrypt(string Text) { return Encrypt(Text, "lixyvip"); } /// <summary> /// 加密数据 /// </summary> /// <param name="Text"></param> /// <param name="sKey"></param> /// <returns></returns> public static string Encrypt(string Text, string sKey) { DESCryptoServiceProvider des = new DESCryptoServiceProvider(); byte[] inputByteArray; inputByteArray = Encoding.Default.GetBytes(Text); des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8)); des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8)); System.IO.MemoryStream ms = new System.IO.MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); StringBuilder ret = new StringBuilder(); foreach (byte b in ms.ToArray()) { ret.AppendFormat("{0:X2}", b); } return ret.ToString(); } #endregion #region ========解密======== /// <summary> /// 解密 /// </summary> /// <param name="Text"></param> /// <returns></returns> public static string Decrypt(string Text) { return Decrypt(Text, "lixyvip"); } /// <summary> /// 解密数据 /// </summary> /// <param name="Text"></param> /// <param name="sKey"></param> /// <returns></returns> public static string Decrypt(string Text, string sKey) { try { DESCryptoServiceProvider des = new DESCryptoServiceProvider(); int len; len = Text.Length / 2; byte[] inputByteArray = new byte[len]; int x, i; for (x = 0; x < len; x++) { i = Convert.ToInt32(Text.Substring(x * 2, 2), 16); inputByteArray[x] = (byte)i; } des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8)); des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8)); System.IO.MemoryStream ms = new System.IO.MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); return Encoding.Default.GetString(ms.ToArray()); } catch { return Text; } } #endregion } 读取当前客户机器所有的Cookies代码: string[] keyArr = HttpContext.Current.Request.Cookies.AllKeys; for (int c = 0; c < keyArr.Length; c++) { Response.Write(HttpContext.Current.Request.Cookies[keyArr[c]].Name); Response.Write("<br />"); Response.Write(HttpContext.Current.Request.Cookies[keyArr[c]].Expires.ToString()); Response.Write("<br />"); Response.Write(HttpContext.Current.Request.Cookies[keyArr[c]].Value); Response.Write("<br />"); Response.Write("<br />"); } 使用或自己写重载方法参考示例:Cookie有三个属性需要注意一下: 1. Domain 域 2. Path 路径 3. Expires 过期时间 跨域操作需要设置域属性: Response.Cookies("MyCookie").Domain = "cnblogs.com"; (这里指的是泛域名) 这样在其它二级域名下就都可以访问到了, ASP 和 ASP.NET 测试通过 虚拟目录下访问: 我在ASP端做了下测试,.NET的没试, 如果不指定Path属性, 不同虚拟目录下Cookie无法共享 将Response.Cookies("MyCookie").Path = "/" 就可以了 总的写法: Response.Cookies("MyCookie").Domain = "cnblogs.com"; Response.Cookies("MyCookie").Path = "/" Response.Cookies("MyCookie").Expires = Now + 365; Response.Cookies("MyCookie")("Test") = "test"; SetUserCookies(SiteInfo.DomainName, "/", "hnce", name, str, DateTime.Now.AddMinutes(miniute)); return GetUserCookies("hnce", name); ClearUserCookies(SiteInfo.DomainName, "/", "hnce"); 另外提醒一下,cookie.Values.Add(_cookiename, _value); 跟 cookie.Values[_cookiename] = _value; 这两种方式都可以设置Cookies的值,但是Add和Set方法后,读取要使用Get方法,而Values[]赋值方式,读取要使用HttpContext.Current.Request.Cookies[_key][_cookiename].ToString() 否则有Cookies读取不了的情况。 //下面附参考文章的部分内容说明 //------------------------------------------------------------------------ 在博客园看到另外一个朋友的可跨二级域操作Cookies方法,在它基础上完善了一下,现发出来 我参考的哪位朋友的文章地址是:http://www.cnblogs.com/voswin/articles/1281520.html 如果无法使用: 请导入命名空间 using System.Security.Cryptography; using System.Text