• session与cookie


    session:

    复制代码
    <form id="form1" runat="server">//这是第一个页面内
            <div>
                用户名:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
                <br />
                密码:<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
                <br />
                <asp:Button ID="Button1" runat="server" Text="Button" /><br />
                <br />
                <asp:Label Style="color: red;" ID="Label1" runat="server" Text=""></asp:Label>
    
            </div>
        </form>
    --------------------------------------------------------------------------- protected void Page_Load(object sender, EventArgs e)
        {
            Button1.Click += Button1_Click;
        }
    
        void Button1_Click(object sender, EventArgs e)
        {
            Label1.Text = "";
            string uname = TextBox1.Text;
            string pwd = TextBox2.Text;
    
            using (DBDataContext con = new DBDataContext())
            {
                Users u = con.Users.Where(r => r.UserName == uname && r.PassWord == pwd).FirstOrDefault();
                if (u != null)
                {
                    //登录成功
                    Session["user"] = u;//这里将从数据库中查到的u放入session 
                    Response.Redirect("default2.aspx");
                }
                else
                {
                    //登录失败
                    Label1.Text = "用户名或密码错误";
                }
            }
    ------------------------------------------------------------------------
       <form id="form1" runat="server">//这是第二个页面
            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
            <asp:Button ID="Button1" runat="server" Text="退出登录" />
        </form>
    ------------------------------------------------------------------------
      protected void Page_Load(object sender, EventArgs e)
        {
            Button1.Click += Button1_Click;
    
            if (Session["user"] != null)
            {
                Users u = Session["user"] as Users;//这里是取session中的的值
                Label1.Text = u.NickName + ",欢迎你!";
            }
            else
            {
                Response.Redirect("default.aspx");
            }
        }
    
        void Button1_Click(object sender, EventArgs e)//退出登录
        {
            Session["user"] = null;//清除session
            Response.Redirect("default.aspx");
        }
    }
    复制代码

    2.Cookie:

    复制代码
     <form id="form1" runat="server">//第一个页面
    
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
            <br />
            <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
            <br />
            <asp:CheckBox ID="CheckBox1" runat="server" Text="3天内保持登录" />
            <br />
            <br />
            <asp:Button ID="Button1" runat="server" Text="登录" />
    
            <input type="button" value="hehe" id="btn1" />
    
        </form>
    ---------------------------------------------------------------------------
    protected void Page_Load(object sender, EventArgs e)
        {
            Button1.Click += Button1_Click;
        }
    
        void Button1_Click(object sender, EventArgs e)
        {
            string uname = TextBox1.Text;
            string pwd = TextBox2.Text;
    
            using (DBDataContext con = new DBDataContext())
            {
                Users u = con.Users.Where(r => r.UserName == uname && r.PassWord == pwd).FirstOrDefault();
                if (u != null)
                {
                    Response.Cookies["aaa"].Value = u.UserName;//给cookie 赋值  注意用Response
                    if (CheckBox1.Checked)
                    {
                        Response.Cookies["aaa"].Expires = DateTime.Now.AddDays(3); //给cookie设置有效周期
                    }
                }
                else
                {
    
                }
            }
    ------------------------------------------------------------------------
     <form id="form1" runat="server">//第二个页面
        <div>
            <h1>这是2页面</h1>
            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
            <asp:Button ID="Button1" runat="server" Text="Button" />
        </div>
        </form>
    ------------------------------------------------------------------------
    protected void Page_Load(object sender, EventArgs e)
        {
            Button1.Click += Button1_Click;
    
            if (Request.Cookies["aaa"] != null)//cookie 的取值 用request
            {
                using (DBDataContext con = new DBDataContext())
                {
                    Users u = con.Users.Where(r => r.UserName == Request.Cookies["aaa"].Value).FirstOrDefault();
                    Label1.Text = u.NickName;
                }
            }
            else
            {
                Response.Redirect("Default.aspx");
            }
        }
    
        void Button1_Click(object sender, EventArgs e)
        {
            Response.Cookies["aaa"].Expires = DateTime.Now.AddDays(-3);//给cookie 回复生命周期
        }
    复制代码

    总结:不同

    Session 保存在服务器内存中
    生命周期:默认 20分钟
    可以存放Object类型
    Seesion["key"] = o;



    Cookie 保存在客户端浏览器缓存中
    会话cookie生命周期: 20分钟
    只能存放 字符串
    Response.Cookies["key"].Value = string;

    取值:
    Rquest.Cookies["key"].Value

    持久cookie
    Response.Cookies["key"].Expires = DateTime.Now.AddDays(int);

    安全性方面:Session很安全    Cookie 很不安全可以用js操作

  • 相关阅读:
    使用 %matplotlib inline 出错?
    RandomForest 调参
    sql中的笛卡尔积
    Sublime text 3 搭建Python3 IDE
    在Windows Python3.5 安装LightGBM
    lodash获取数组或对象的值 at
    lodash 移除数据元素 pull without 删除数组元素
    js 常用类型转换简写
    UTC时间格式转换
    CSS Flexible 布局兼容性以及解决方案
  • 原文地址:https://www.cnblogs.com/gbbwzz/p/8086637.html
Copyright © 2020-2023  润新知