提供创建和操作各 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. python3 class类 练习题

    """一.定义一个学生Student类.有下面的类属性:1 姓名 name2 年龄 age3 成绩 score(语文,数学,英语) [每课成绩的类型为整数] 类方法:1 ...

  2. 使用xadmin更新数据时,报错expected string or bytes-like object

    expected string or bytes-like object 期望的字符串或类似字节的对象,一般为数据类型不匹配造成 本人在实际项目里发现的问题是: 数据库里的字段类型与django里mo ...

  3. XAMPP之Mysql启动失败

    启动XAMPP中的Mysql出现如下: 可能的原因是本地有多个MySQL,所以要在注册表编辑器中将imagePath改成XAMPP中的mysql的地址.(打开注册表编辑器:win+R,输入regedi ...

  4. TCGA数据批量下载

    由于经常需要涉及到TCGA数据的分析,我简单的整理了一下数据批量下载的文件后缀. cancer_name <- "SKCM" output_path <- paste0 ...

  5. VC6无法生成Release版本程序

    在工程设置,将Setting for后面的选项改为Win32 Release.然后重新编译.结果仍然没有生成release,而且打开设置时,依然是Win32 Debug. 解决办法,在VC6.0的工具 ...

  6. java入门---运算符&算术运算符&自增自减运算符&关系运算符&位运算符

        计算机的最基本用途之一就是执行数学运算,作为一门计算机语言,Java也提供了一套丰富的运算符来操纵变量.我们可以把运算符分成以下几组: 算术运算符 关系运算符 位运算符 逻辑运算符 赋值运算符 ...

  7. 20155316 实验三《敏捷开发与XP实践》实验报告

    实验1 实验内容 在IDEA中使用工具(Code->Reformate Code)把下面代码重新格式化,再研究一下Code菜单,找出一项让自己感觉最好用的功能.提交截图,加上自己学号水印. pu ...

  8. C语言复习20170821

    函数 函数头部参数表里的变量称为形参,也是内部变量,只能在函数体内访问. 形参的作用是实现主调函数与被调函数之间的联系,通常将函数所处理的数据,影响函数功能的因素或者函数处理的结果作为形参.没有形参的 ...

  9. sql语句-8-sql学习流程

  10. day3 直方图

    1.绘制直方图 # coding=utf-8 import cv2 import numpy as np from matplotlib import pyplot as plt img1 = cv2 ...