c# Cookie的操作
#region Cookie的操作:Cookie的读取、Cookie的保存
/// <summary>
/// 静态方法:保存Cookie
/// </summary>
/// <param name="Hash">内容</param>
/// <param name="_remberme">存cookie天数</param>
/// <param name="_CookieName">cookie名</param>
public static void SaveCookie(Hashtable Hash, int _remberme, string _CookieName)
{
string DoMain = "";
string[] Arr = HttpContext.Current.Request.Url.Host.Split('.');
for (int i = 1; i < Arr.Length; i++)
{
DoMain += "." + Arr[i];
}
if (_CookieName == "")
{
_CookieName = ConfigurationManager.AppSettings["CookieName"];
}
HttpCookie Cookie = new HttpCookie(_CookieName);
foreach (DictionaryEntry entry in Hash)
{
Cookie[entry.Key.ToString()] = entry.Value.ToString();
}
//Cookie.Domain = ConfigurationManager.AppSettings["CookieDoMain"];
Cookie.Expires = DateTime.UtcNow.AddDays(_remberme);
Cookie.Domain = DoMain;
HttpContext.Current.Response.Cookies.Add(Cookie);
}
/// <summary>
/// 静态方法:读取Cookie的值
/// </summary>
/// <param name="_name">Cookie key</param>
/// <returns></returns>
public static string GetCookie(string _name)
{
HttpCookie Cookie = HttpContext.Current.Request.Cookies.Get(ConfigurationManager.AppSettings["CookieName"]);
if (Cookie != null)
{
string DoMain = "";
string[] Arr = HttpContext.Current.Request.Url.Host.Split('.');
for (int i = 1; i < Arr.Length; i++)
{
DoMain += "." + Arr[i];
}
Cookie.Domain = DoMain;
return Cookie[_name];
}
else
{
return "";
}
}
/// <summary>
/// 静态方法:读取Cookie的值
/// </summary>
/// <param name="_name">Cookie key</param>
/// <param name="_CookieName">cookie名</param>
/// <returns></returns>
public static string GetCookie(string _name, string _CookieName)
{
if (_CookieName == "")
{
_CookieName = ConfigurationManager.AppSettings["CookieName"];
}
HttpCookie Cookie = HttpContext.Current.Request.Cookies.Get(_CookieName);
if (Cookie != null)
{
string DoMain = "";
string[] Arr = HttpContext.Current.Request.Url.Host.Split('.');
for (int i = 1; i < Arr.Length; i++)
{
DoMain += "." + Arr[i];
}
Cookie.Domain = DoMain;
return Cookie[_name];
}
else
{
return "";
}
}
#endregion