• cookie 传值


    cookie

    保存在客户端上的一套临时文件 每一个浏览器都有它自己独立的cookie

    不是特别安全

    登录的状态保持 不是重要的数据缓存
    不占用服务器资源
    会话cookie 浏览器一关就没了/20分钟的生命周期
    持久cookie 设置过期时间

    //会话cookie

    //20分钟自动过期

      Response.Cookies["user"].Value = u.UserName;

    //持久

      cookie Response.Cookies["user"].Expires = DateTime.Now.AddDays(3);

    //清除

      Cookie Response.Cookies["user"].Expires = DateTime.Now.AddDays(-10);

     Defayil.aspx页面

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title></title>
        <style type="text/css">
            .div1 {
                position: absolute;
                 400px;
                height: 300px;
                background-color: white;
                box-shadow: 0px 0px 15px #808080;/*上下左右无偏移  15px的扩散*/
                left: 50%;
                margin-left: -200px;
                top: 50%;
                margin-top: -170px;
                text-align: center;
                line-height: 40px;
            }
    
            #TextBox1 {
                margin-top: 100px;
            }
    
            #Button1 {
                 100px;
                height: 30px;
                border: 0px;
                background-color: navy;
                color: white;
            }
    
            #Label1 {
                color: red;
            }
        </style>
    </head>
    <body style="position: fixed; height: 100%;  100%; background-color: #eeeeee;">
        <form id="form1" runat="server">
            <div class="div1">
                用户名:
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
                密 码:
                <asp:TextBox ID="TextBox2" TextMode="Password" runat="server"></asp:TextBox><br />
                <asp:CheckBox ID="CheckBox1" runat="server" Text="三天内保持登录状态" /><br />
                <asp:Button ID="Button1" runat="server" Text="登 录" /><br />
                <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
    
            </div>
        </form>
    </body>
    </html>

    右键c#代码

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Button1.Click += Button1_Click;//按钮的点击事件
        }
    
        void Button1_Click(object sender, EventArgs e)
        {
            //1、获取用户输入的用户名密码
            string uname = TextBox1.Text;
            string pwd = TextBox2.Text;
    
            //2、验证用户名密码
            Users u = new UsersData().SelectUser(uname, pwd);
    
            Label1.Text = "";//不管登陆成不成功清空下
            //3、执行相关操作
            if (u != null)
            {
                Response.Cookies["user"].Value = u.UserName;//只能存英文不能存汉字汉字会被编译成乱码
    
                if (CheckBox1.Checked) //CheckBox1选中状态不选中的话临时保存关掉浏览器再次登陆需要密码
                {
                    Response.Cookies["user"].Expires = DateTime.Now.AddDays(3);//密码用户名保存3天关掉浏览器再次登陆不需要密码
                }
    
                Response.Redirect("Default2.aspx");//登陆成功后跳转Default2.aspx页面
            }
            else//登陆失败
            {
                Label1.Text = "用户名或密码错误!!!";
            }
    
    
    
    
    
        }
    }

  • 相关阅读:
    Django框架第九篇--Django和Ajax、序列化组件(serializers)、自定义分页器、模型表choice参数
    Django框架之第五篇(模板层) --变量、过滤器(|)、标签(% %)、自定义标签、过滤器、inclusion_tag,模板的继承、模板的注入、静态文件
    Django框架学习易错和易忘点
    守护线程
    Thread其他属性和方法
    进程与线程的区别
    开启线程
    关闭屏幕输出分屏显示
    生产者消费者模型
    队列
  • 原文地址:https://www.cnblogs.com/skyhorseyk/p/7297785.html
Copyright © 2020-2023  润新知