1.有一个登陆页面login.aspx,默认需要用户输入用户名为admin,密码为123,并且有一个是否记住我的选项。

2.当用户不勾选记住我的时候,只是利用Session来保存登陆状态,并且跳转到index.aspx页面,在Session没有过期的时候,用户无需重复登陆,访问login.aspx页面的时候能直接进入index.aspx页面。

3.当用户勾选记住我后,默认利用cookie保存其用户名及密码,cookie的有效期为7天,当cookie没有过期时,用户在任意的页面都能直接登陆并且记录Session。(没看懂划线部分,没实现这个功能

4.在index.aspx页面设计一个退出按钮,退出的时候清空session以及cookie。

login.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="login.aspx.cs" Inherits="_2022._10._10.login" %><!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>
</head>
<body><form id="form1" runat="server"><div>用户名  <asp:TextBox ID="UserName" runat="server"></asp:TextBox><br />密 码  <asp:TextBox ID="UserPassword" runat="server"></asp:TextBox><br /><asp:CheckBox ID="Radio" runat="server" Text="是否记住我" /><br /><asp:Button ID="btn_Login" runat="server" Text="登陆" OnClick="btn_Login_Click" /></div></form>
</body>
</html>

login.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace _2022._10._10
{public partial class login : System.Web.UI.Page{const int num = 5;//用户上限数量为5public struct User          //定义结构体{public string name;public string password;}User[] MyUser = new User[num];//开结构体数组protected void Page_Load(object sender, EventArgs e)//预加载{//数组初始赋值for (int i = 0; i < num; i++){MyUser[i].name = "admin";MyUser[i].password = "123";}if(Session["go_Name"] != null && Session["go_Password"] != null){if (Session["go_Name"].ToString() == "admin" && Session["go_Password"].ToString() == "123"){Response.Redirect("index.aspx");}}if (Request.Cookies["go_Name"]!=null && Request.Cookies["go_Password"] != null){if (Request.Cookies["go_Name"].Value == "admin" && Request.Cookies["go_Password"].Value == "123"){Response.Redirect("index.aspx");}}}protected void btn_Login_Click(object sender, EventArgs e){int j = 0;string name = UserName.Text;string password = UserPassword.Text;for(int i = 0; i < num; i++)//与数组中的所有数据进行判断,是否与其中一个相同{if(name==MyUser[i].name && password == MyUser[i].password){if (Radio.Checked==false)//未勾选 使用session存储{Session["go_Name"] = name;Session["go_Password"] = password;Response.Redirect("index.aspx");}else//未勾选 使用cookie存储{Response.Cookies["go_Name"].Value = name;Response.Cookies["go_Password"].Value = password;Response.Cookies["go_Name"].Expires = DateTime.Now.AddDays(7);Response.Cookies["go_Password"].Expires = DateTime.Now.AddDays(7);Response.Redirect("index.aspx");}}j++;}if (j == num){UserName.Text = "输入错误";UserPassword.Text = "输入错误";}}}
}

index.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="index.aspx.cs" Inherits="_2022._10._10.index" %><!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>
</head>
<body><form id="form1" runat="server"><div><asp:Button ID="btn_UnRem" runat="server" Text="忘记我(删除所有cookie和session)" OnClick="btn_UnRem_Click" /></div></form>
</body>
</html>

index.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;namespace _2022._10._10
{public partial class index : System.Web.UI.Page{protected void Page_Load(object sender, EventArgs e){Response.Write("账户:");if(Request.Cookies["go_Name"]!=null)Response.Write(Request.Cookies["go_Name"].Value);if (Session["go_Name"] != null)Response.Write(Session["go_Name"]);Response.Write("密码:");if (Request.Cookies["go_Password"] != null)Response.Write(Request.Cookies["go_Password"].Value);if (Session["go_Password"] != null)Response.Write(Session["go_Password"]);}protected void btn_UnRem_Click(object sender, EventArgs e){//cookie全部清空Response.Cookies["go_Name"].Expires = DateTime.Now.AddDays(-1);Response.Cookies["go_Password"].Expires = DateTime.Now.AddDays(-1);//session全部清空Session.Clear();Response.Redirect("login.aspx");}}
}

【web】session和cookie写登录页面,且免登陆功能和清空功能。相关推荐

  1. Java Web —— Session 和 cookie 保存登录信息

    session 与 cookie cookie 与 session 应用于互联网中的一项基本技术--会话(客户端与服务端的交互)跟踪技术,用来跟踪用户的整个会话.简单来说,cookie 是通过在客户端 ...

  2. Asp.net 2.0 用Membership自己写登录页面

    用户登录页面 如果用MS自带的登录控件,不用改一行代码,而且会自带一个数据库,但是界面什么的无法布局,当然也可以编辑布局,但是不怎么方便,我们可以自己用table来写登录页面,以下是遇到的几个问题及解 ...

  3. FiddlerScript 注入cooke实现自动登录,Fiddler实现免登陆,浏览器通代理实现自动登录

    一,问题描述 最近看到一个用易语言写的软件,点击运行,会自动将电脑的代理修改为:http=127.0.0.1:xxx;https=127.0.0.1:xxx ,使用chrome浏览器或火狐浏览器配置这 ...

  4. Web Session和Cookie

    前言 session和cookie对于web开发来说,即陌生有熟悉,熟悉是指经常碰到,比如request.getSession(),request.getCookie(),session.setAtt ...

  5. PHP 超简单的SESSION与COOKIE制作登录验证

    第一步,制作一个提交信息的表单页面 这里我不过多叙述,都能懂的 把他命名为login.php <!DOCTYPE html> <html> <head><me ...

  6. Ajax Session失效跳转登录页面的方法

    在Struts应用中,我们发出的请求都会经过 相应的拦截器进行相关处理,一般都会有一个用户登录拦截(Session失效拦截):一般请求的话,如果Session失效时,我们会跳到登录页面,可是如果我们采 ...

  7. Web收银台系统[项目] -- (3) 登录页面

    1. 登录页面的过程: 输入用户名和密码点击登录, 点击完登录之后, 还是要交给相应的servlet, 接着这个登录的servlet会去操作数据库, 进行查询一下当前要登录的用户在数据库中是否存在, ...

  8. Cookie的应用---十天免登陆设计

    首先要创建一个数据库的表,表内含有用户名和密码,其代码如下: drop table if exists t_user; create table t_user(id int(10) primary k ...

  9. session过期跳转登录页面

    2019独角兽企业重金招聘Python工程师标准>>> 项目需要做一个自动登出的功能,查询了网上的资料,一开始准备用session监听做,按照下面方式配置监听器 1.在项目的web. ...

最新文章

  1. 全球最大AI商业展会开幕,这家老牌巨头担当中国唯一代表
  2. mysql学习笔记06分组语句的使用
  3. 【剑指offer】二叉搜索树转双向链表,C++实现
  4. Python Mysql学习总结
  5. golang windows环境下的配置安装
  6. 2021年中国乙酸异冰片酯市场趋势报告、技术动态创新及2027年市场预测
  7. Biztalk中Host Instance线程控制
  8. linux atoi,atoi()的替代办法
  9. 抖音上很火的 立方体相册和旋转时钟,基于人脸识别实现程序员的专属相册和专属时钟,包含15套相册模板和9套时钟风格,可以直接替换成自己的图片,部署生成自己的个性化专属相册
  10. 怎么看rx580是不是470刷的_rx580显卡看是不是刷的教程
  11. Java修仙,法力无边(光速回顾Java基础~)
  12. Go | 限流器实现
  13. Lenovo笔记本各类型触控板,触摸部分只能移动无法点击的问题汇总
  14. 厉害了,分布式数据库中间件ShardingSphere毕业成为Apache顶级项目!
  15. 玩转OSGI-ApacheFelix(一)框架启动部署
  16. 序列标注的BIO标注体系
  17. discuz!x 应用中心更新为新地址解决方案之一
  18. Fiddler 的几个用法
  19. 3、Prism的使用二
  20. 利用Vulhub复现log4j漏洞

热门文章

  1. Sysdiagnose在iOS能耗测试中的应用
  2. hud抬头显示器哪个好_车萝卜又一新品,智能HUD蓝牙版1S抬头显示器上手评测
  3. 邢福有老师 找准公文写作要点,避开这3种毛病
  4. 异步社区|本周半价电子书书单
  5. 【adb命令】通过电脑连接手机,输入adb devices命令报错error: cannot connect to daemon的解决办法
  6. 口语中常用的连读总结(地道英语总结)
  7. SILK : SILK_RTP_PayloadFormat 中文翻译
  8. 个人项目需求和分析------日程管理APP
  9. 微软开源项目-AI修复老照片
  10. JMH204剑网2+精品怀旧端游【剑侠情缘2】降龙端优化汉化+任务GM工具+视频安装教程