asp.net 조작 쿠키 클래스, 데이터테이블 대량 저장 쿠키 포함
17993 단어 Datatable
public class CookieMgr {
#region Cookie
/// <summary>
/// Cookie
/// </summary>
/// <param name="dt"> </param>
/// <param name="cookieName">Cookie </param>
/// <param name="day"> , </param>
public static void SaveCookie(DataTable dt, string cookieName, int day) {
HttpCookie cookie = new HttpCookie(cookieName);
if (day != 0)
cookie.Expires = DateTime.Now.AddDays(day);
for (int i = 0; i < dt.Columns.Count; i++) {
cookie.Values.Add(dt.Columns[i].ColumnName, StringMgr.Encrypt(dt.Rows[0][dt.Columns[i].ColumnName].ToString())); //
}
HttpContext.Current.Response.AppendCookie(cookie);
}
#endregion
#region Cookie
/// <summary>
/// Cookie
/// </summary>
/// <param name="cookieName">cookie </param>
/// <param name="day"> , </param>
public static void InitCookie(string cookieName, int day) {
HttpCookie cookie = new HttpCookie(cookieName);
if (day != 0)
cookie.Expires = DateTime.Now.AddDays(day);
cookie.Values.Add("initExpires", StringMgr.Encrypt(day.ToString()));
HttpContext.Current.Response.Cookies.Add(cookie);
}
#endregion
#region Cookie ,
/// <summary>
/// Cookie ,
/// </summary>
/// <param name="cookieName">Cookie </param>
/// <param name="key"> </param>
/// <param name="value"> </param>
public static void SetCookie(string cookieName, string key, string value) {
HttpCookie cookie = null;
if (HttpContext.Current.Request.Cookies[cookieName] != null) {
cookie = HttpContext.Current.Request.Cookies[cookieName];
} else
cookie = new HttpCookie(cookieName);
bool b = false;
for (int i = 0; i < cookie.Values.Keys.Count; i++) {
if (cookie.Values.Keys[i] == key) {
b = true;
break;
}
}
value = StringMgr.Encrypt(value);
if (b)
cookie.Values[key] = value;
else
cookie.Values.Add(key, value);
for (int i = 0; i < cookie.Values.Keys.Count; i++) {
if (cookie.Values.Keys[i] == "initExpires") {
cookie.Expires = DateTime.Now.AddDays(int.Parse(StringMgr.Decrypt(cookie.Values["initExpires"].ToString())));
break;
}
}
HttpContext.Current.Response.Cookies.Add(cookie);
}
#endregion
#region Cookie
/// <summary>
/// Cookie
/// </summary>
/// <param name="cookieName">Cookie </param>
/// <param name="key"> </param>
/// <returns> 【CookieName】 【key】 </returns>
public static string GetCookie(string cookieName, string key) {
HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName];
if (cookie != null && cookie.Values[key] != null)
return StringMgr.Decrypt(cookie.Values[key].ToString());
else
return Param.CookieErrorString;
}
#endregion
#region Cookie
/// <summary>
/// Cookie
/// </summary>
/// <param name="cookieName">Cookie </param>
/// <param name="key"> </param>
public static void RemoveCookie(string cookieName, string key) {
HttpCookie cookie = null;
if (HttpContext.Current.Request.Cookies[cookieName] != null)
cookie = HttpContext.Current.Request.Cookies[cookieName];
else
cookie = new HttpCookie(cookieName);
bool b = false;
for (int i = 0; i < cookie.Values.Keys.Count; i++) {
if (cookie.Values.Keys[i] == key) {
b = true;
break;
}
}
if (b)
cookie.Values.Remove(key);
for (int i = 0; i < cookie.Values.Keys.Count; i++) {
if (cookie.Values.Keys[i] == "initExpires") {
cookie.Expires = DateTime.Now.AddDays(int.Parse(StringMgr.Decrypt(cookie.Values["initExpires"].ToString())));
break;
}
}
HttpContext.Current.Response.Cookies.Add(cookie);
}
#endregion
#region Cookie
/// <summary>
/// Cookie
/// </summary>
/// <param name="cookieName">Cookie </param>
public static void ClearCookie(string cookieName) {
System.Web.HttpCookie cookie = new System.Web.HttpCookie(cookieName);
cookie.Expires = System.DateTime.Now.AddYears(-1);
System.Web.HttpContext.Current.Response.AppendCookie(cookie);
}
#endregion
#region Cookie
/// <summary>
/// Cookie
/// </summary>
/// <param name="cookieName">Cookie </param>
/// <returns> Cookie </returns>
public static NameValueCollection GetCookieValues(string cookieName) {
if (HttpContext.Current.Request.Cookies[cookieName] != null) {
HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName];
return cookie.Values;
} else
return null;
}
#endregion
}
DT를 쿠키에 저장
ookieMgr.SaveCookie(dt, 이름, 0),
쿠키 키 값 가져오기
CookieMgr.GetCookie(이름, 이름);
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
yui--datatable 행 추가 형식텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.