/// <summary>
/// Cookies赋值
/// </summary>
/// <param name="strName">主键</param>
/// <param name="strValue">键值</param>
/// <param name="strDay">有效天数</param>
/// <returns></returns>
public static bool setCookie(string strName, string strValue, int strDay)
{
try
{
HttpCookie Cookie = new HttpCookie(strName);
Cookie.Expires = DateTime.Now.AddDays(strDay);
Cookie.Value = Convert.ToBase64String(System.Text.Encoding.GetEncoding("UTF-8").GetBytes(strValue));
System.Web.HttpContext.Current.Response.Cookies.Add(Cookie);
return true;
}
catch
{
return false;
}
}
/// <summary>
/// 读取Cookies
/// </summary>
/// <param name="strName">主键</param>
/// <returns></returns>
public static string getCookie(string strName)
{
HttpCookie Cookie = System.Web.HttpContext.Current.Request.Cookies[strName];
if (Cookie != null)
{
return System.Text.Encoding.GetEncoding("UTF-8").GetString(Convert.FromBase64String(Cookie.Value.ToString()));
}
else
{
return null;
}
}
以上是处理的方法,主要问题就出现在“Convert.ToBase64String”和“Convert.FromBase64String”上,在32位系统下直接对COOKIES进行赋值和读取的操作(汉字)不会产生任何影响,但在64位下就会出现乱码。
此问题困扰了博主很长一段时间,最后无意间搜索才发现有这种解决方法。