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

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");
        }
    }
}

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值