zoukankan      html  css  js  c++  java
  • Cookie application session

    •Application 对象是存储于服务器的全局变量
    •Cookie 存储信息于客户端
    •Session 对象用于在服务器端存储用户的信息,在用户结束会话时被清除

    1.将信息写入Cookies 中/

    方式一
    //HttpCookie hcName = new HttpCookie("UserName", this.txtName.Text);
    //HttpCookie hcPwd = new HttpCookie("UserPwd", this.txtPassword.Text);
    //hcName.Expires = DateTime.Now.AddDays(double.Parse(this.ddlDays.SelectedValue));
    //hcPwd.Expires = DateTime.Now.AddDays(double.Parse(this.ddlDays.SelectedValue));

    //this.Response.Cookies.Add(hcName);
    //this.Response.Cookies.Add(hcPwd);

    读取

    HttpCookie hcPwd = this.Request.Cookies["UserPwd"];
    string s = "";
    s = hcPwd.Value;

    方式二

    HttpCookie hc = new HttpCookie("User");
    hc.Values.Add("name", this.txtName.Text.Trim());
    hc.Values.Add("pwd", this.txtPassword.Text.Trim());
    hc.Expires = DateTime.Now.AddDays(double.Parse(this.ddlDays.SelectedValue));
    //将cookie对象写入客户端
    this.Response.Cookies.Add(hc);

    读取

    HttpCookie hc = this.Request.Cookies["User"];

    string s = "";

    if (hc != null)
    {
    s += "从Cookies中读取的姓名是:" + hc.Values["name"].ToString() + "<br/>读取的密码是:" + hc.Values["pwd"].ToString();
    }
    else
    {
    s += "没有Cookie对象!";
    }

    2 删除cookie

  • 相关阅读:
    88. 合并两个有序数组
    680. 验证回文字符串 Ⅱ
    345. 反转字符串中的元音字母
    633. 平方数之和
    分支程序设计
    scanf函数(初学者)
    输入与输出(初学者)
    C语句详细(初学者)
    算术运算符和算术表达式(初学者)
    变量赋值(初学者)
  • 原文地址:https://www.cnblogs.com/ChineseMoonGod/p/3729839.html
Copyright © 2011-2022 走看看