62,047
社区成员
发帖
与我相关
我的任务
分享
/// <summary>
/// Set Cookies
/// </summary>
/// <param name="iMinutes">Cookie Expired Minutes</param>
/// <returns>true if the cookie was successfully added; false otherwise.</returns>
public static bool SetCookies(string Key, string Value, int iMinutes)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies[Key];
if (cookie == null)
{
cookie = new HttpCookie(Key, Value);
//cookie.Domain = "/";
}
else
{
cookie.Value = Value;
}
cookie.Expires = DateTime.Now.AddMinutes(iMinutes);
System.Web.HttpContext.Current.Response.Cookies.Add(cookie);
System.Web.HttpContext.Current.Request.Cookies.Add(cookie);
return true;
}
/// <summary>
/// Get Coolies string Value, null if no this cookie.
/// </summary>
/// <param name="Key">Cookie Name</param>
public static string GetCookies(string Key)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies[Key];
if (cookie != null)
{
return cookie.Value;
}
else
{
return null;
}
}
/// <summary>
/// Set Cookies
/// </summary>
/// <param name="iMinutes">Cookie Expired Minutes</param>
/// <returns>true if the cookie was successfully added; false otherwise.</returns>
public static bool SetCookies(string Key, string Value, int iMinutes)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies[Key];
if (cookie == null)
{
cookie = new HttpCookie(Key, HttpUtility.UrlEncode(Value));
//cookie.Domain = "/";
}
else
{
cookie.Value = HttpUtility.UrlEncode(Value);
}
cookie.Expires = DateTime.Now.AddMinutes(iMinutes);
System.Web.HttpContext.Current.Response.Cookies.Add(cookie);
System.Web.HttpContext.Current.Request.Cookies.Add(cookie);
return true;
}
/// <summary>
/// Get Coolies string Value, null if no this cookie.
/// </summary>
/// <param name="Key">Cookie Name</param>
public static string GetCookies(string Key)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies[Key];
if (cookie != null)
{
return HttpUtility.UrlDecode(cookie.Value);
}
else
{
return null;
}
}
HttpCookie cookie=new HttpCookie("Test");
cookie.Values.Add("Test1",HttpUtility.UrlEncode("cookie写入"));
Response.AppendCookie(cookie);
//读取
HttpCookie cookie=Request.Cookies["Test"];
string simple1=HttpUtility.UrlDecode(cookie["Test1"]);