HttpCookie Class
提供创建和操作各 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的更多相关文章
- httpCookie与Cookie安全
Web 应用程序使用的 Cookie 个人认为这里设置的cookie与访问cookie的安全性关联大一点,配置节如下 <httpCookies domain="String" ...
- ahjesus在asp.net中还可以通过设置HttpCookie对象的过期时间为DateTime.MinValue来指定此Cookies为跟随浏览器生效
ahjesus在asp.net中还可以通过设置HttpCookie对象的过期时间为DateTime.MinValue来指定此Cookies为跟随浏览器生效
- web.config中的HttpCookie.HttpOnly属性
Abstract: The program does not set the HttpCookie.HttpOnly property to true. Explanation: The defaul ...
- HttpCookie加匿名类实现多语言
突然想做一个多语言网站,确不知道怎么实现好,突然想到了HttpCookie,然后页面后台用匿名类实现语言的储存. string lan = Request["str_lan"]; ...
- (转)Asp.net的HttpCookie写入汉字读取时为乱...
今天有个问我:在Asp.net的HttpCookie中写入汉字,读取值为什么全是乱码?其实这是因为文字编码而造成的,汉字是两个编码,所以才会搞出这么个乱码出来!其实解决的方法很简单:只要在写入Cook ...
- HttpCookie类
转自:http://www.cnblogs.com/kissdodog/archive/2013/01/08/2851937.html HttpCookie类专门由C#用于读取和写入Cookie的类. ...
- 笔记整理--Http-Cookie
如何设置一个永远无法删除的Cookie -- 系统架构 -- IT技术博客大学习 -- 共学习 共进步! - Google Chrome (2013/6/20 9:46:38) 如何设置一个永远无法删 ...
- Jmeter(八)HTTPCookie管理器
Cookie绝对是日常工作以及技术中一个绕不过去的‘角色’,正常各种各样的业务需要Cookie的存在.Jmeter中也有支持发送Cookie的组件,但是,仅是后话:在此还是有必要先记一记Cookie到 ...
- System.Web.HttpCookie.cs
ylbtech-System.Web.HttpCookie.cs 1.程序集 System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken= ...
随机推荐
- 基于 HTML5 WebGL 的 3D 水泥工厂生产线
前言 今天为大家带来一个很酷的作品,依然运用了强大的 HT for Web 的 3D 图形组件,动作流畅性能好,大家可以先来欣赏一下效果! 点我进入! 整体风格为科技金属风,制作精良,由于上传 gif ...
- error:0906D064:PEM routines:PEM_read_bio:bad base64 decode
今天在使用easywechat对接企业打款到银行卡时,遇到了两个错误 error:0906D064:PEM routines:PEM_read_bio:bad base64 decode 和 erro ...
- 飞控入门之C语言结构体、枚举
结构体 先来说明一下,结构体存在的意义.比如说有一只猫,要在C语言程序中综合描述它,那么可以这样说,它的体重是float类型的,颜色是char类型的,它的一些食物名字是一个数组,那么如果分开定义这些变 ...
- 移植ARM linux下远程连接工具dropbear
移植ARM linux下远程连接工具dropbear 原文地址:http://www.cnblogs.com/NickQ/p/9010529.html 移植zlib 下载地址:https://gith ...
- 在Linux Mint 19 / Linux Mint 18上安装VirtualBox 6.0 / 5.2
如果你直接可以 sudo apt-get install virtualbox-6.0那就相安无事 否则参考https://www.itzgeek.com/how-tos/linux/linux-mi ...
- java入门---运算符&算术运算符&自增自减运算符&关系运算符&位运算符
计算机的最基本用途之一就是执行数学运算,作为一门计算机语言,Java也提供了一套丰富的运算符来操纵变量.我们可以把运算符分成以下几组: 算术运算符 关系运算符 位运算符 逻辑运算符 赋值运算符 ...
- C++与C#的多态
C++ 多态 多态按字面的意思就是多种形态.当类之间存在层次结构,并且类之间是通过继承关联时,就会用到多态. C++ 多态意味着调用成员函数时,会根据调用函数的对象的类型来执行不同的函数. 下面的实例 ...
- addClass+siblings+removeClass用意:
$(this).addClass("li_add").siblings().removeClass("li_add").children('.floor2'). ...
- slqite3练习
连接 import sqlite3 con = sqlite3.connect(":memory:") c = con.cursor() # Create table c.exec ...
- day1 Opencv安装 python 2.7 (32位)
[参考安装步骤] http://opencv-python-tutroals.readthedocs.io/en/latest/index.html http://blog.csdn.net/huru ...