/// <summary>
///CookiesHelper 的摘要说明
/// </summary>
public class CookiesHelper
{
public CookiesHelper()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
#region 设定cookie
/// <summary>
/// 设定cookie
/// </summary>
/// <param name="cookiename"></param>
/// <param name="cookievalue"></param>
public static void SetCookie(string cookiename, string cookievalue)
{
SetCookie(cookiename, cookievalue, DateTime.MinValue);
}
public static void SetCookie(string cookiename, string cookievalue, DateTime expires)
{
if (cookiename != null && cookievalue != null)
{
HttpCookie cookie = new HttpCookie(cookiename);
cookie.Value = (cookievalue.Length == 0) ? String.Empty : System.Web.HttpUtility.UrlEncode(cookievalue);
if (expires > DateTime.MinValue)
cookie.Expires = expires;
HttpContext.Current.Response.Cookies.Add(cookie);
}
}
#endregion

#region 清除指定cookie
/// <summary>
/// 清除指定cookie
/// </summary>
/// <param name="cookiename"></param>
public static void ClearCookie(string cookiename)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies[cookiename];
if (cookie != null)
{
cookie.Expires = DateTime.Now.AddYears(-3);
HttpContext.Current.Response.Cookies.Add(cookie);
}
}
#endregion

#region 得到指定的Cookie值
/// <summary>
/// 得到指定的Cookie值
/// </summary>
/// <param name="cookiename"></param>
public static string GetCookieValue(string cookiename)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies[cookiename];
string cookievalue = String.Empty;
if (cookie != null)
{
cookievalue = cookie.Value;
}
if (cookievalue.Length > 0)
cookievalue = HttpUtility.UrlDecode(cookievalue);
return cookievalue;
}
#endregion

static HttpCookie hc;
static HttpCookieCollection hccol;
/// <summary>
/// 设置cookie
/// </summary>
/// <param name="name">名称</param>
/// <param name="value">值</param>
/// <param name="expire">过期时间,以分钟为单位</param>
public static void setCookie(string name, string value, double expire)
{
hc = new HttpCookie(name);
hc.Value = value;
hc.Expires = DateTime.Now.AddMinutes(expire);
HttpContext.Current.Response.AppendCookie(hc);
}
/// <summary>
/// 设置cookie
/// </summary>
/// <param name="name">名称</param>
/// <param name="value">值</param>
/// <param name="expire">过期时间,以天为单位</param>
public static void setCookie(string name, string value, int days)
{
hc = new HttpCookie(name);
hc.Value = value;
hc.Expires = DateTime.Now.AddDays(days);
HttpContext.Current.Response.AppendCookie(hc);
}
/// <summary>
/// 获取cookie
/// </summary>
/// <param name="name">名称</param>
/// <returns>值</returns>
public static string getCookie(string name)
{
hc = HttpContext.Current.Request.Cookies[name];
return hc == null ? null : hc.Value;
}

/// <summary>
/// 清除cookie
/// </summary>
/// <param name="name">名称</param>
public static void clearCookie(string name)
{
hc = new HttpCookie(name);
hc.Expires = DateTime.Today.AddDays(-1);
HttpContext.Current.Response.SetCookie(hc);
}

/// <summary>
/// 清空cookie
/// </summary>
public static void clearAllCookies()
{
hccol = new HttpCookieCollection();
foreach (HttpCookie hc0 in hccol)
{
hc0.Expires = DateTime.Today.AddDays(-1);
HttpContext.Current.Response.SetCookie(hc);
}
}
}

CookiesHelper的更多相关文章

  1. 【.NET】Cookie操作类

    public static class CookiesHelper { /// <summary> /// Cookies赋值 /// </summary> /// <p ...

  2. ASP.NET Cache缓存的用法

    本文导读:在.NET运用中经常用到缓存(Cache)对象.有HttpContext.Current.Cache以及HttpRuntime.Cache,HttpRuntime.Cache是应用程序级别的 ...

  3. asp.net 自带的缓存

    本文导读:在.NET运用中经常用到缓存(Cache)对象.有HttpContext.Current.Cache以及HttpRuntime.Cache,HttpRuntime.Cache是应用程序级别的 ...

  4. .NET的Cookie相关操作

    using System; using System.Collections.Generic; using System.Text; using System.Web; namespace Comm ...

  5. 【转】ASP.NET Cookies简单应用 记住用户名和密码

    不要试图给Password类型的TextBox赋值! 在asp.net中,不要试图给Password类型的TextBox控件赋值! 无论是在设计或是运行时,都不可以的. 猜测的原因是,password ...

  6. 微信公众号H5支付步骤

    微信公众平台:https://mp.weixin.qq.com/ 进入 微信支付 管理>开通支付功能. 微信支付|商户平台: 设置安全目录:https://pay.weixin.qq.com/i ...

  7. C#中发送邮件,包含Html代码 CDO.Message

    C#代码: /// <summary> /// 发送邮件 /// </summary> /// <param name="context">&l ...

  8. 获取验证码效果和后台代码(js+html+cs)

    客户端js+html代码 <script type="text/javascript"> var tcode = 0;//定时器返回代码 //获得验证码 functio ...

  9. [转]ASP.NET cache缓存的用法

    本文转自:https://blog.csdn.net/mss359681091/article/details/51076712 本文导读:在.NET运用中经常用到缓存(Cache)对象.有HttpC ...

随机推荐

  1. Vue 封装js

    //封装模块化文件 新建的.js文件 var storage = { set(key, value) { localStorage.setItem(key, JSON.stringify(value) ...

  2. REST 风格

  3. Distill详述「可微图像参数化」:神经网络可视化和风格迁移利器!

    近日,期刊平台 Distill 发布了谷歌研究人员的一篇文章,介绍一个适用于神经网络可视化和风格迁移的强大工具:可微图像参数化.这篇文章从多个方面介绍了该工具. 图像分类神经网络拥有卓越的图像生成能力 ...

  4. 《Linux内核精髓:精通Linux内核必会的75个绝技》目录

    1章 内核入门HACK #1 如何获取Linux内核HACK #2 如何编译Linux内核HACK #3 如何编写内核模块HACK #4 如何使用GitHACK #5 使用checkpatch.pl检 ...

  5. JS 更新

    JavaScript概述 ECMAScript和JavaScript的关系 1996年11月,JavaScript的创造者--Netscape公司,决定将JavaScript提交给国际标准化组织ECM ...

  6. 网站搜索引擎优化SEO策略及相关工具资源

    网站优化的十大奇招妙技 1. 选择有效的关键字: 关键字是描述你的产品及服务的词语,选择适当的关键字是建立一个高排名网站的第一步.选择关键字的一个重要的技巧是选取那些常为人们在搜索时所用到的关键字. ...

  7. php7---redis

    wget https://github.com/phpredis/phpredis/archive/php7.zip cd /usr/local/src/ phpredis-php7 /usr/loc ...

  8. HTML5 Canvas ( 事件交互, 点击事件为例 ) isPointInPath

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  9. mysql数据库复制

    核心命令是 myssqldump mysqldump --host=host1 --opt sourceDb| mysql --host=host2 -C targetDb 详情参考: MySQL数据 ...

  10. Spring boot Tomcat配置

    来自: https://www.cnblogs.com/a8457013/p/7687764.html