提供创建和操作各 HTTP Cookie 的类型安全方法。

 #region 写入指定Cookie的值 +static void WriteCookie(string cookieName, string data, DateTime expires)
/// <summary>
/// 写入指定Cookie的值
/// </summary>
/// <param name="cookieName">cookie名称</param>
/// <param name="data">cookie值</param>
/// <param name="expires">过期时间</param>
public static void WriteCookie(string cookieName, string data, DateTime expires)
{
HttpCookie cookie = new HttpCookie(cookieName);
if (HttpContext.Current.Request.Url.Host.Contains(DOMAIN))
{
cookie.Domain = DOMAIN;
}
cookie.Expires = expires;
cookie.Value = HttpContext.Current.Server.UrlEncode(data);
HttpContext.Current.Response.Cookies.Add(cookie);
}
#endregion

官网示例:

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

    protected void Page_Load(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
// Get cookie from the current request.
HttpCookie cookie = Request.Cookies.Get("DateCookieExample"); // Check if cookie exists in the current request.
if (cookie == null)
{
sb.Append("Cookie was not received from the client. ");
sb.Append("Creating cookie to add to the response. <br/>");
// Create cookie.
cookie = new HttpCookie("DateCookieExample");
// Set value of cookie to current date time.
cookie.Value = DateTime.Now.ToString();
// Set cookie to expire in 10 minutes.
cookie.Expires = DateTime.Now.AddMinutes(10d);
// Insert the cookie in the current HttpResponse.
Response.Cookies.Add(cookie);
}
else
{
sb.Append("Cookie retrieved from client. <br/>");
sb.Append("Cookie Name: " + cookie.Name + "<br/>");
sb.Append("Cookie Value: " + cookie.Value + "<br/>");
sb.Append("Cookie Expiration Date: " +
cookie.Expires.ToString() + "<br/>");
}
Label1.Text = sb.ToString();
}
</script> <html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>HttpCookie Example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label id="Label1" runat="server"></asp:Label>
</div>
</form>
</body>
</html>

HttpCookie Class的更多相关文章

  1. httpCookie与Cookie安全

    Web 应用程序使用的 Cookie 个人认为这里设置的cookie与访问cookie的安全性关联大一点,配置节如下 <httpCookies domain="String" ...

  2. ahjesus在asp.net中还可以通过设置HttpCookie对象的过期时间为DateTime.MinValue来指定此Cookies为跟随浏览器生效

    ahjesus在asp.net中还可以通过设置HttpCookie对象的过期时间为DateTime.MinValue来指定此Cookies为跟随浏览器生效

  3. web.config中的HttpCookie.HttpOnly属性

    Abstract: The program does not set the HttpCookie.HttpOnly property to true. Explanation: The defaul ...

  4. HttpCookie加匿名类实现多语言

    突然想做一个多语言网站,确不知道怎么实现好,突然想到了HttpCookie,然后页面后台用匿名类实现语言的储存. string lan = Request["str_lan"]; ...

  5. (转)Asp.net的HttpCookie写入汉字读取时为乱...

    今天有个问我:在Asp.net的HttpCookie中写入汉字,读取值为什么全是乱码?其实这是因为文字编码而造成的,汉字是两个编码,所以才会搞出这么个乱码出来!其实解决的方法很简单:只要在写入Cook ...

  6. HttpCookie类

    转自:http://www.cnblogs.com/kissdodog/archive/2013/01/08/2851937.html HttpCookie类专门由C#用于读取和写入Cookie的类. ...

  7. 笔记整理--Http-Cookie

    如何设置一个永远无法删除的Cookie -- 系统架构 -- IT技术博客大学习 -- 共学习 共进步! - Google Chrome (2013/6/20 9:46:38) 如何设置一个永远无法删 ...

  8. Jmeter(八)HTTPCookie管理器

    Cookie绝对是日常工作以及技术中一个绕不过去的‘角色’,正常各种各样的业务需要Cookie的存在.Jmeter中也有支持发送Cookie的组件,但是,仅是后话:在此还是有必要先记一记Cookie到 ...

  9. System.Web.HttpCookie.cs

    ylbtech-System.Web.HttpCookie.cs 1.程序集 System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken= ...

随机推荐

  1. redis集群步骤(windows环境)

    注:原文转自 https://blog.csdn.net/zsg88/article/details/73715947 ,仅用作为方便查阅 一 所需软件:Redis.Ruby语言运行环境.Redis的 ...

  2. Python-type函数使用

  3. Python基础学习笔记(一)安装以及IDE的配置

    一.初识Python Python官方网站:www.python.org 版本:python-3.4.3.amd64 初次接触Python,没有使用Python 2.x进行开发真实项目的经历.所以不知 ...

  4. SSL&TlS握手

    SSL/TLS简介 •SSL:安全套接字层(secure socket layer) •TLS:传输层安全协议(transport layer security) SSL和TLS都是加密协议,旨在基于 ...

  5. C语言编程学习不难学,是你没找对方法!

    C语言是面向过程的,而C++是面向对象的 C和C++的区别: C是一个结构化语言,它的重点在于算法和数据结构.C程序的设计首要考虑的是如何通过一个过程,对输入(或环境条件)进行运算处理得到输出(或实现 ...

  6. FPGA静态时序分析基础

    FPGA静态时序分析基础 基本概念 Skew: 时钟偏移 Skew表示时钟到达不同触发器的延时差别,Tskew = 时钟到达2号触发器的时刻 - 时钟到达1号触发器的时刻. Jitter: 时钟抖动 ...

  7. C语言复习20170728

    C语言复习20170728 键盘输入和屏幕输出 字符常量:把字符放在一对单引号内,适用于多数可打印字符. 转义字符: 以反斜线()开头,也是放在一对单引号内,适用于控制字符. .\t,是水平制表符,相 ...

  8. day2 CSS- 选择器

    1.CSS 语法 css是英文Cascading Style Sheets的缩写,称为层叠样式表 2.css的四种引入方式 1.行内式 行内式是在标记的style属性中设定CSS样式.这种方式没有体现 ...

  9. PL/SQL编辑数据"这些查询结果不可更新,请包括ROWID或使用SELECT...FOR UPDATE获得可更新结果"处理

    只要有人用了: select t.* from 表名  t where 字段=xxx  for update 而不是: select t.rowid,t.* from 表名  t where 字段=x ...

  10. Maven学习(十六)-----Maven存储库

    什么是Maven资源库? 在 Maven 术语里存储库是一个目录,即目录中保存所有项目的 jar 库,插件或任何其他项目特定文件,并可以容易由 Maven 使用. Maven库中有三种类型 local ...