asp.net下cookies操作完美代码_实用技巧

复制代码 代码如下:

using System;
using System.Web;
namespace Moosoft.OA.Public
{
/// <summary>
/// Cookie帮助类
/// </summary>
public class CookiesHelper
{
#region 获取Cookie
/// <summary>
/// 获得Cookie的值
/// </summary>
/// <param name="cookieName"></param>
/// <returns></returns>
public static string GetCookieValue(string cookieName)
{
return GetCookieValue(cookieName, null);
}
/// <summary>
/// 获得Cookie的值
/// </summary>
/// <param name="cookieName"></param>
/// <param name="key"></param>
/// <returns></returns>
public static string GetCookieValue(string cookieName, string key)
{
HttpRequest request = HttpContext.Current.Request;
if (request != null)
return GetCookieValue(request.Cookies[cookieName], key);
return "";
}
/// <summary>
/// 获得Cookie的子键值
/// </summary>
/// <param name="cookie"></param>
/// <param name="key"></param>
/// <returns></returns>
public static string GetCookieValue(HttpCookie cookie, string key)
{
if (cookie != null)
{
if (!string.IsNullOrEmpty(key) && cookie.HasKeys)
return cookie.Values[key];
else
return cookie.Value;
}
return "";
}
/// <summary>
/// 获得Cookie
/// </summary>
/// <param name="cookieName"></param>
/// <returns></returns>
public static HttpCookie GetCookie(string cookieName)
{
HttpRequest request = HttpContext.Current.Request;
if (request != null)
return request.Cookies[cookieName];
return null;
}
#endregion
#region 删除Cookie
/// <summary>
/// 删除Cookie
/// </summary>
/// <param name="cookieName"></param>
public static void RemoveCookie(string cookieName)
{
RemoveCookie(cookieName, null);
}
/// <summary>
/// 删除Cookie的子键
/// </summary>
/// <param name="cookieName"></param>
/// <param name="key"></param>
public static void RemoveCookie(string cookieName, string key)
{
HttpResponse response = HttpContext.Current.Response;
if (response != null)
{
HttpCookie cookie = response.Cookies[cookieName];
if (cookie != null)
{
if (!string.IsNullOrEmpty(key) && cookie.HasKeys)
cookie.Values.Remove(key);
else
response.Cookies.Remove(cookieName);
}
}
}
#endregion
#region 设置/修改Cookie
/// <summary>
/// 设置Cookie子键的值
/// </summary>
/// <param name="cookieName"></param>
/// <param name="key"></param>
/// <param name="value"></param>
public static void SetCookie(string cookieName, string key, string value)
{
SetCookie(cookieName, key, value, null);
}
/// <summary>
/// 设置Cookie值
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
public static void SetCookie(string key, string value)
{
SetCookie(key, null, value, null);
}
/// <summary>
/// 设置Cookie值和过期时间
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="expires"></param>
public static void SetCookie(string key, string value, DateTime expires)
{
SetCookie(key, null, value, expires);
}
/// <summary>
/// 设置Cookie过期时间
/// </summary>
/// <param name="cookieName"></param>
/// <param name="expires"></param>
public static void SetCookie(string cookieName, DateTime expires)
{
SetCookie(cookieName, null, null, expires);
}
/// <summary>
/// 设置Cookie
/// </summary>
/// <param name="cookieName"></param>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="expires"></param>
public static void SetCookie(string cookieName, string key, string value, DateTime? expires)
{
HttpResponse response = HttpContext.Current.Response;
if (response != null)
{
HttpCookie cookie = response.Cookies[cookieName];
if (cookie != null)
{
if (!string.IsNullOrEmpty(key) && cookie.HasKeys)
cookie.Values.Set(key, value);
else
if (!string.IsNullOrEmpty(value))
cookie.Value = value;
if (expires != null)
cookie.Expires = expires.Value;
response.SetCookie(cookie);
}
}
}
#endregion
#region 添加Cookie
/// <summary>
/// 添加Cookie
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
public static void AddCookie(string key, string value)
{
AddCookie(new HttpCookie(key, value));
}
/// <summary>
/// 添加Cookie
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="expires"></param>
public static void AddCookie(string key, string value, DateTime expires)
{
HttpCookie cookie = new HttpCookie(key, value);
cookie.Expires = expires;
AddCookie(cookie);
}
/// <summary>
/// 添加为Cookie.Values集合
/// </summary>
/// <param name="cookieName"></param>
/// <param name="key"></param>
/// <param name="value"></param>
public static void AddCookie(string cookieName, string key, string value)
{
HttpCookie cookie = new HttpCookie(cookieName);
cookie.Values.Add(key, value);
AddCookie(cookie);
}
/// <summary>
/// 添加为Cookie集合
/// </summary>
/// <param name="cookieName">Cookie名称</param>
/// <param name="expires">过期时间</param>
public static void AddCookie(string cookieName, DateTime expires)
{
HttpCookie cookie = new HttpCookie(cookieName);
cookie.Expires = expires;
AddCookie(cookie);
}
/// <summary>
/// 添加为Cookie.Values集合
/// </summary>
/// <param name="cookieName"></param>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="expires"></param>
public static void AddCookie(string cookieName, string key, string value, DateTime expires)
{
HttpCookie cookie = new HttpCookie(cookieName);
cookie.Expires = expires;
cookie.Values.Add(key, value);
AddCookie(cookie);
}
/// <summary>
/// 添加Cookie
/// </summary>
/// <param name="cookie"></param>
public static void AddCookie(HttpCookie cookie)
{
HttpResponse response = HttpContext.Current.Response;
if (response != null)
{
//指定客户端脚本是否可以访问[默认为false]
cookie.HttpOnly = true;
//指定统一的Path,比便能通存通取
cookie.Path = "/";
//设置跨域,这样在其它二级域名下就都可以访问到了
//cookie.Domain = "chinesecoo.com";
response.AppendCookie(cookie);
}
}
#endregion
}
}

时间: 2024-10-22 08:12:42

asp.net下cookies操作完美代码_实用技巧的相关文章

asp.net XML文件操作实现代码_实用技巧

以前也学过一些这方面的知识,好久都没怎么用了,忘得也差不多,正好现在可以重新巩固一遍,熟悉一下对XML文件的操作. XML(Extensible Markup Language)即可扩展标记语言,它与HTML一样,都是SGML(Standard Generalized Markup Language,标准通用标记语言).Xml是Internet环境中跨平台的,依赖于内容的技术,是当前处理结构化文档信息的有力工具. 扩展标记语言XML是一种简单的数据存储语言,使用一系列简单的标记描述数据,而这些标

ASP与ASP.NET互通COOKIES的一点经验_实用技巧

  在微软推出.NET并进行了大规模的推广普及之后,ASP.NET逐渐进入了信息化系统开发的主流.但与此同时,而用ASP开发的旧系统面则临被整合,这时,面临一个问题:ASP与ASP.NET互相整合时,其中文COOKIES信息无法被互通共享,当使用ASP.NET写入中文COOKIES信息后,使用ASP进行读取,读出来的却是乱码,而非中文.    后来通过查找资料,不停地实践,终于找到了问题的根源,中文COOKIES信息在ASP中无法被正确读取得原因为其中文编码格式不同.    开发项目Web.co

在.ashx文件中获取cookies值实现代码_实用技巧

创建 复制代码 代码如下: HttpCookie cookies = new HttpCookie("validate"); cookies["validate"] = validateNum; Response.AppendCookie(cookies); 获取 复制代码 代码如下: if (context.Request.Cookies["validate"] != null) { validate = context.Request.Coo

ASP.NET下对cookies的操作实现代码_实用技巧

复制代码 代码如下: public class BsCookie { //操作的cookie private HttpCookie _theCookie; //对应的cookie的名称 private string _cookieName; private bool _httpOnly = true; /// <summary> /// 是否只允许在服务器端访问,默认只允许在服务端访问 /// </summary> public bool HttpOnly { get { retu

asp.net下Cache 缓存操作类代码_实用技巧

复制代码 代码如下: using System.Collections.Generic; using System.Web; using System; namespace DataAccess { /// <summary> /// 缓存控制类 /// </summary> public class CacheControl { public static List<string> AllUseCacheKey = new List<string>();

asp.net DropDownList 三级联动下拉菜单实现代码_实用技巧

复制代码 代码如下: if (!IsPostBack) { //一级分类列表 this.DropDownList1.DataSource = dsbb.SelectSubjct1(); this.DropDownList1.DataTextField = "cName"; this.DropDownList1.DataValueField = "Ccode"; this.DropDownList1.DataBind(); this.DropDownList1.Ite

ASP.NET对IIS中的虚拟目录进行操作的代码_实用技巧

复制代码 代码如下: //假如虚拟目录名为"Webtest",先在项目中引用 //System.DirectoryServices.dll,再 using System.DirectoryServices; protected System.DirectoryServices.DirectoryEntry dirroot; 1.添加新的虚拟目录 复制代码 代码如下: DirectoryEntry newVirDir = dirroot.Children.Add("Webtes

asp.net配置会话状态Session实现代码_实用技巧

下面来详细说明: 复制代码 代码如下: <sessionState timeout="timeout in minutes" cookieless="[true|false]" mode="Off|InProc|StateServer|SQLServer" stateConnectionString="tcpip=server:port" stateNetworkTimeout="for network ope

asp.net导出EXCEL的功能代码_实用技巧

复制代码 代码如下: //由gridviw导出为Excel public static void ToExcel(System.Web.UI.Control ctl) { HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=Excel.xls"); HttpContext.Current.Response.Charset = "UTF-8