在博客园看到另外一个朋友的可跨二级域操作Cookies方法,在它基础上完善了一下,现发出来
我参考的哪位朋友的文章地址是:http://www.cnblogs.com/voswin/articles/1281520.html
/// 添加/更新 Cookies
/// </summary>
/// <param name="_domain"></param>
/// <param name="_cookiepath"></param>
/// <param name="_key"></param>
/// <param name="_cookiename"></param>
/// <param name="_value"></param>
public static void SetUserCookies(string _domain, string _cookiepath, string _key, string _cookiename, string _value, DateTime _expires)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies[_key];
//防止中文乱码
_value = System.Web.HttpUtility.UrlEncode(_value);
//加密
_value = DESEncrypt.Encrypt(_value);
if (cookie == null)
{
cookie = new HttpCookie(_key);
cookie.Domain = _domain;
cookie.Path = _cookiepath;
cookie.Expires = _expires;
cookie.Values.Add(_cookiename, _value);
HttpContext.Current.Response.AppendCookie(cookie);
}
else
{
cookie.Domain = _domain;
cookie.Path = _cookiepath;
cookie.Expires = _expires;
if (cookie.Values[_cookiename] != null)
{
cookie.Values.Set(_cookiename, _value);
}
else
{
cookie.Values.Add(_cookiename, _value);
}
HttpContext.Current.Response.SetCookie(cookie);
}
}
/// <summary>
/// 读取Cookies
/// </summary>
/// <param name="_key"></param>
/// <param name="_cookiename"></param>
/// <returns></returns>
public static string GetUserCookies(string _key, string _cookiename)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies[_key];
if (cookie != null)
{
string _value = cookie.Values.Get(_cookiename);
if (!string.IsNullOrEmpty(_value))
{
//防止中文乱码
_value = System.Web.HttpUtility.UrlDecode(_value);
//解密
_value = DESEncrypt.Decrypt(_value);
return _value;
}
else
{
return "";
}
}
else
{
return "";
}
}
/// <summary>
/// 清除Cookies
/// </summary>
/// <param name="_domain"></param>
/// <param name="_cookiepath"></param>
/// <param name="key"></param>
public static void ClearUserCookies(string _domain, string _cookiepath, string _key)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies[_key];
if (cookie != null)
{
cookie.Values.Clear();
cookie.Domain = _domain;
cookie.Path = _cookiepath;
cookie.Expires = DateTime.Now.AddDays(-1);
HttpContext.Current.Response.SetCookie(cookie);
}
}
加密Cookies和解密Cookies方法:
读取当前客户机器所有的Cookies代码:
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 />");
}
使用或自己写重载方法参考示例:
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读取不了的情况。
//下面附参考文章的部分内容说明
//------------------------------------------------------------------------
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";