C#操作Cookie
2015-12-29 14:10
537 查看
/* 创建者:菜刀居士的博客
* 创建日期: 2014年09月02号
* 功能:操作Cookie
*
*/
namespace Net.String.ConsoleApplication
{
using System;
using System.Web;
public static class CookieHelper
{
/// <summary>
/// 加入cookie
/// </summary>
public static void AddCookie(this HttpContext h,string name, string value)
{
HttpCookie cookieName = new HttpCookie(name, System.Web.HttpUtility.UrlEncode(value, System.Text.Encoding.GetEncoding(65001)));
h.Response.Cookies.Add(cookieName);
}
/// <summary>
/// 加入cookie
/// </summary>
public static void AddCookie(this HttpContext h,string name, string value, TimeSpan span)
{
HttpCookie cookieName = new HttpCookie(name, System.Web.HttpUtility.UrlEncode(value, System.Text.Encoding.GetEncoding(65001)));
cookieName.Expires = DateTime.Now.Add(span);
h.Response.Cookies.Add(cookieName);
}
/// <summary>
/// 得到cookie
/// </summary>
public static string GetCookie(this HttpContext h, string name)
{
if (h.Request.Cookies[name] != null)
{
if (h.Response.Cookies.Count > 0 && h.Response.Cookies[name] != null)
{
return System.Web.HttpUtility.UrlDecode(h.Response.Cookies[name].Value, System.Text.Encoding.GetEncoding(65001));
}
return System.Web.HttpUtility.UrlDecode(h.Request.Cookies[name].Value, System.Text.Encoding.GetEncoding(65001));
}
else
{ return string.Empty; }
}
/// <summary>
/// 删除cookie
/// </summary>
public static void RemoveCookie(this HttpContext h,string name)
{
h.Response.Cookies[name].Value = null;
h.Response.Cookies[name].Expires = DateTime.Now.AddDays(-1);
}
/// <summary>
/// 清空cookie
/// </summary>
public static void ClearCookie(this HttpContext h)
{
try
{
foreach (HttpCookie hc in h.Response.Cookies)
{
hc.Value = null;
hc.Expires = DateTime.Now.AddDays(-1);
}
}
catch { }
}
}
}
* 创建日期: 2014年09月02号
* 功能:操作Cookie
*
*/
namespace Net.String.ConsoleApplication
{
using System;
using System.Web;
public static class CookieHelper
{
/// <summary>
/// 加入cookie
/// </summary>
public static void AddCookie(this HttpContext h,string name, string value)
{
HttpCookie cookieName = new HttpCookie(name, System.Web.HttpUtility.UrlEncode(value, System.Text.Encoding.GetEncoding(65001)));
h.Response.Cookies.Add(cookieName);
}
/// <summary>
/// 加入cookie
/// </summary>
public static void AddCookie(this HttpContext h,string name, string value, TimeSpan span)
{
HttpCookie cookieName = new HttpCookie(name, System.Web.HttpUtility.UrlEncode(value, System.Text.Encoding.GetEncoding(65001)));
cookieName.Expires = DateTime.Now.Add(span);
h.Response.Cookies.Add(cookieName);
}
/// <summary>
/// 得到cookie
/// </summary>
public static string GetCookie(this HttpContext h, string name)
{
if (h.Request.Cookies[name] != null)
{
if (h.Response.Cookies.Count > 0 && h.Response.Cookies[name] != null)
{
return System.Web.HttpUtility.UrlDecode(h.Response.Cookies[name].Value, System.Text.Encoding.GetEncoding(65001));
}
return System.Web.HttpUtility.UrlDecode(h.Request.Cookies[name].Value, System.Text.Encoding.GetEncoding(65001));
}
else
{ return string.Empty; }
}
/// <summary>
/// 删除cookie
/// </summary>
public static void RemoveCookie(this HttpContext h,string name)
{
h.Response.Cookies[name].Value = null;
h.Response.Cookies[name].Expires = DateTime.Now.AddDays(-1);
}
/// <summary>
/// 清空cookie
/// </summary>
public static void ClearCookie(this HttpContext h)
{
try
{
foreach (HttpCookie hc in h.Response.Cookies)
{
hc.Value = null;
hc.Expires = DateTime.Now.AddDays(-1);
}
}
catch { }
}
}
}
相关文章推荐
- c#中的partial
- 1、c#对XML文件的解析
- 【C#】 之 out和ref
- C# RDLC 绑定
- C# Regex 正则表达式 匹配特定的内容
- C# RGB颜色转换为16进制
- C# 用 * 输出两个等腰三角形组成的菱形
- C# 使用 DirectoryInfo 递归指定目录中的所有目录及文件
- C# 打开指定的文件夹 记住路径中 / 与 \ 的用法
- 带有按钮并且可以执行单击事件的WINFORM窗体,体悟C#的创建过程
- C# 入门之 Hello World
- C#动态编译
- 【机房收费系统C#版】——导出Excel
- c#读取xml配置文件
- C#序列化和反序列化代码总结
- C#中xml序列化和反序列化
- c#中SOAP序列化和反序列化
- C#中委托
- C# 发送邮件 qq与163
- C#通用之文件