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;//用户上限数量为5
public 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");
}
}
}