Cookie记住账号

本文介绍如何通过Cookie来实现网站账号的自动登录功能,让用户在不删除缓存的情况下,下次访问时无需再次输入账号和密码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Cookie记住账号

给大家分享的功能是Cookie记住账号
效果是记住账号后每次都可以直接登录不用再次输入账号密码(在不清除网页缓存的情况下)

效果图:
在这里插入图片描述

MVC代码:
//登陆页面

public ActionResult Login()
{
	string Account = "";
	string Password = "";
	bool isRember = false;

	HttpCookie cookie = System.Web.HttpContext.Current.Request.Cookies["operator"]
	if (cookie != null)
	{
		if (cookie["Account"] != null)
		{
			Account=System.Web.HttpContext.Current.Server.UrlDecode(cookie["Account"]);
		}
		if (cookie["Password"] != null)
		{
			Password=System.Web.HttpContext.Current.Server.UrlDecode(cookie["Password"]);
		}
		isRember = true;
	}

	ViewBag.Account = Account;
	ViewBag.Password = Password;
	ViewBag.isRember = isRember;
	return View();
}

//验证登录

public ActionResult CheckLogin(D_Operator _Operator)
{
	ReturnJson msg = new ReturnJson();
	string Account = Request.Form["Account"];
	string Password = Request.Form["Password"];
	string StrRember = Request.Form["RememberMe"];

	try
	{
		D_Operator Operator = (from tb in myModels.D_Operator
								where tb.OperatorNumber.ToString() == Account.Trim()
								select tb).Single();
		string AsePassword = Common.AESEncryptHelper.Encrypt(Password);
		if (Operator.OperatorPassword.Equals(AsePassword.Trim()))
		{
			Session["OperatorID"] = Operator.OperatorID;
			Session["Operator"] = Operator.Operator;
			if (StrRember == "true")
			{
        		HttpCookie cookie = new HttpCookie("operator");
        		cookie.Expires = DateTime.Now.AddDays(7);
        		cookie["Account"] = Account;
        		cookie["Password"] = Password;
    			Response.Cookies.Add(cookie);
			}
			else
			{
        		HttpCookie cookie = new HttpCookie("operator");
        		cookie.Expires = DateTime.Now.AddDays(-1);
  				Response.Cookies.Add(cookie);
			}
			msg.State = true;
		}
		else
		{
			msg.Text = "密码错误!";
		}
	}
	catch (Exception)
	{
		msg.Text = "用户不存在!";
	}
	return Json(msg, JsonRequestBehavior.AllowGet);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值