惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

N
News and Events Feed by Topic
S
SegmentFault 最新的问题
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Last Week in AI
Last Week in AI
Jina AI
Jina AI
H
Help Net Security
C
Check Point Blog
aimingoo的专栏
aimingoo的专栏
MyScale Blog
MyScale Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Vercel News
Vercel News
L
LangChain Blog
Recorded Future
Recorded Future
F
Full Disclosure
Google DeepMind News
Google DeepMind News
Microsoft Security Blog
Microsoft Security Blog
I
InfoQ
GbyAI
GbyAI
B
Blog RSS Feed
T
The Blog of Author Tim Ferriss
Engineering at Meta
Engineering at Meta
A
About on SuperTechFans
M
MIT News - Artificial intelligence
爱范儿
爱范儿
V
V2EX
Microsoft Azure Blog
Microsoft Azure Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Y
Y Combinator Blog
B
Blog
WordPress大学
WordPress大学
Blog — PlanetScale
Blog — PlanetScale
W
WeLiveSecurity
MongoDB | Blog
MongoDB | Blog
Cloudbric
Cloudbric
N
News and Events Feed by Topic
The Cloudflare Blog
月光博客
月光博客
博客园 - 三生石上(FineUI控件)
有赞技术团队
有赞技术团队
D
DataBreaches.Net
博客园 - 【当耐特】
T
Troy Hunt's Blog
V
Visual Studio Blog
V2EX - 技术
V2EX - 技术
Apple Machine Learning Research
Apple Machine Learning Research
博客园 - 司徒正美
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Google Online Security Blog
Google Online Security Blog
The GitHub Blog
The GitHub Blog

博客园 - 白天的萤火虫

Net开源框架/项目,你研究过几个? 主题:实用且不花哨的js代码大全 关于模拟注册登录的 字字带泪-写在三十岁到来这一天 关于silverlight的安装 推荐一个IE下的优秀js调试工具(Companion.JS) 给分页控件增加一列 编号 关于Ajax的web配置 - 白天的萤火虫 - 博客园 一个行转列的例子 获取HTML代码 - 白天的萤火虫 - 博客园 最新Silverlight开发环境配置介绍 sql server中的存储过程调试 PetShop介绍集锦 DIV+CSS实现圆角 button 绑定 enter键 转载---使用Ajax实现DropDownList和ListBox的联动以及两个ListBox之间数据的移动 自己调试通过的存储过程 又一个通用分页存储过程,支持表别名,多表联合查询SQL语句--转载 CascadingDropDown从数据库中读取数据绑定到DropDownList控件上
写入和读取cookie
白天的萤火虫 · 2008-06-02 · via 博客园 - 白天的萤火虫

Cookies 提供了一种在 Web 应用程序中存储特定用户信息(如历史记录或用户偏好)的方式。Cookie 是连同请求和回应一起在 Web 服务器和客户端之间来回传送的少量文本。Web 应用程序能够在用户访问网站的时候读取 Cookie 中所包含的信息。

浏览器负责对客户端计算机中的 Cookies 进行管理。Cookies 是使用 HttpResponse 对象被发送到客户端的,该对象暴露了一个名为 Cookies 的属性集合。任何你想要在 Web 应用程序中发送到浏览器的 Cookies 都必须被添加到这个集合中。在你写入一个新 Cookie 的时候,你必须指定 NameValue 属性。每个 Cookie 都必须拥有一个唯一的名称,这样 Web 应用程序才能够在浏览器的未来请求中对它进行识别。

把 Cookie 写入到用户计算机中有两种方式。你既能够直接在 Cookies 集合中设置 Cookie 的属性,也能够创建一个新的 HttpCookie 对象实例并把它添加到 Cookies 集合中。你必须在 ASP.NET 页面被呈现到客户端之前创建 Cookies。例如,你可以在 Page_Load 事件处理器中写入一个 Cookie,但是不能够在 Page_Unload 事件处理器中写入 Cookie。更多关于页面生命周期的信息,请参考:[ASP.NET 页面生命周期概览]。

更多信息,请参考:[ASP.NET Cookies 概览]。

Cookies 集合中设置属性来写入 Cookie

  • 在你想要写入 Cookie 的 ASP.NET 页面中,在 Cookies 集合中指定 Cookie 的属性。

    如下代码实例说明了一个名为 UserSettings 的 Cookie,并为子键 Font 和 Color 设置了值。同时也把失效时间设置成了明天。

    Response.Cookies["UserSettings"]["Font"] = "Arial";
        Response.Cookies["UserSettings"]["Color"] = "Blue";
        Response.Cookies["UserSettings"].Expires = DateTime.Now.AddDays(1d);
        

创建 HttpCookie 对象的实例来写入 Cookie

  1. 创建 HttpCookie 类型的一个对象实例并为其指定名称。

  2. 指定 Cookie 子键中的值并设置 Cookie 的属性。

  3. 把这个 Cookie 添加到 Cookies 集合中。

    如下代码实例说明了一个名为 myCookie 的 HttpCookie 对象实例,用来展示一个名为 UserSettings 的 Cookie。

    HttpCookie myCookie = new HttpCookie("UserSettings");
        myCookie["Font"] = "Arial";
        myCookie["Color"] = "Blue";
        myCookie.Expires = DateTime.Now.AddDays(1d);
        Response.Cookies.Add(myCookie);
        

健壮编程

默认时,Cookies 在相同域中的所有页面中被共享,但是你能够通过设置它们的 Path 属性的方式把 Cookies 限制到一个特定的子目录。要允许 Cookie 能够被应用程序所有目录中的所有页面所访问,请在应用程序根目录中的页面中对它进行设置,并且不要设置 Path 属性。

如果你没有指定 Cookie 的有效期,那么这个 Cookie 将无法在客户计算机中被持续保持,并且会连同用户的会话状态一起失效。

Cookies 只能够存储 String 类型的值。你必须在存储到 Cookie 之前把任何非字符串的值转换成字符串。大部分数据类型通过调用 ToString 方法就能够实现。更多关于数据类型转换成字符串的信息,请参考相应类型的[ToString 方法]。

安全性

不要在 Cookie 中存储机密信息(如用户名称或密码)。更多关于 Cookie 安全性的信息请参考:[ASP.NET Cookies 概览]。

    写入和读取cookie

private void SaveCookie(string CookieName,string CookieValue)
  {
   HttpCookie myCookie = new HttpCookie(CookieName);
   DateTime now = DateTime.Now;

   // Set the cookie value.
   myCookie.Value = CookieValue;
   // Set the cookie expiration date.
   myCookie.Expires = now.AddYears(1);
   if(this.Response.Cookies[CookieName]!=null)
    this.Response.Cookies.Remove(CookieName);
   // Add the cookie.
   this.Response.Cookies.Add(myCookie);
  }
  private string GetCookie(string CookieName)
  {
   HttpCookie myCookie = new HttpCookie(CookieName);
   myCookie = Request.Cookies[CookieName];

   // Read the cookie information and display it.
   if (myCookie != null)
    return myCookie.Value;
   else
    return null;
  }


  #region Web 窗体设计器生成的代码
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {   
   this.Button1.Click += new System.EventHandler(this.Button1_Click);
   this.Button2.Click += new System.EventHandler(this.Button2_Click);
   this.Load += new System.EventHandler(this.Page_Load);

  }
  #endregion

  private void Button1_Click(object sender, System.EventArgs e)
  {
  SaveCookie("mycookie","网际浪子");
  }

  private void Button2_Click(object sender, System.EventArgs e)
  {
   Response.Write(GetCookie("mycookie"));
  }