/// <summary>
/// 设置 Cookie
/// </summary>
/// <param name="lxfs"></param>
/// <param name="expiresDays"></param>
public static void SetCookie(string key, string value, int expiresDays)
{
DateTime expires = DateTime.Now.AddDays(expiresDays);
HttpCookie MyCookie = new HttpCookie(key);
MyCookie.Domain = ".yourdomain.com";
MyCookie.Value = HttpUtility.UrlEncode(value);
MyCookie.Expires = expires;
HttpContext.Current.Response.Cookies.Add(MyCookie);
}
/// 设置 Cookie
/// </summary>
/// <param name="lxfs"></param>
/// <param name="expiresDays"></param>
public static void SetCookie(string key, string value, int expiresDays)
{
DateTime expires = DateTime.Now.AddDays(expiresDays);
HttpCookie MyCookie = new HttpCookie(key);
MyCookie.Domain = ".yourdomain.com";
MyCookie.Value = HttpUtility.UrlEncode(value);
MyCookie.Expires = expires;
HttpContext.Current.Response.Cookies.Add(MyCookie);
}
读取cookie的时候使用这个函数:
/// <summary>
/// 对cookie进行UTF编码
/// author:jake
/// bt:080414
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string GetCookid(string str)
{
Encoding stre = Encoding.GetEncoding("UTF-8");
return HttpUtility.UrlDecode(str,stre);
}
/// 对cookie进行UTF编码
/// author:jake
/// bt:080414
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string GetCookid(string str)
{
Encoding stre = Encoding.GetEncoding("UTF-8");
return HttpUtility.UrlDecode(str,stre);
}
如:
GetCookid(HttpContext.Current.Request.Cookies["sswoo_user"].Value)
如此以来即可解决中文字符cookie丢失的问题了!