C#中Cookie的存取操作
#region Cookie的操作:Cookie的读取、Cookie的保存
/// <summary>
/// 静态方法:保存Cookie
/// </summary>
/// <param name="_name">名称</param>
/// <param name="_value">参数</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"];
if (_remberme == 1)
{
Cookie.Expires=DateTime.UtcNow.AddDays(30);
}
else
{
Cookie.Expires=DateTime.UtcNow.AddDays(1);
}
Cookie.Domain = DoMain;
HttpContext.Current.Response.Cookies.Add(Cookie);
}
/// <summary>
/// 静态方法:读取Cookie的值
/// </summary>
/// <param name="_name"></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"></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
/// <summary>
/// 静态方法:保存Cookie
/// </summary>
/// <param name="_name">名称</param>
/// <param name="_value">参数</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"];
if (_remberme == 1)
{
Cookie.Expires=DateTime.UtcNow.AddDays(30);
}
else
{
Cookie.Expires=DateTime.UtcNow.AddDays(1);
}
Cookie.Domain = DoMain;
HttpContext.Current.Response.Cookies.Add(Cookie);
}
/// <summary>
/// 静态方法:读取Cookie的值
/// </summary>
/// <param name="_name"></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"></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