cookie的使用和记住密码

后台:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebApplication1.Models;

namespace WebApplication1.Controllers
{
    public class LoginController : Controller
    {
        // GET: Login
        public ActionResult Index()
        {
            //获取cookie
            HttpCookie getCookie = Request.Cookies["user"];
            userinfo user = new userinfo() { username = "",pwd = "",IsChecked="" };
            //判断cookie是否为null
            if (getCookie != null)
            {
                user.username = getCookie.Values["username"];
                user.pwd = getCookie.Values["pwd"];
                user.IsChecked = "checked";
            }
            return View(user);
        }

        [HttpPost]
        public ActionResult IsLogin(userinfo user)
        {   
            //user.username = Request["username"];
            //user.pwd = Request["pwd"];
            //判断是否选中记住密码
            if (user.IsChecked == "on")
            {
                //创建一个cookie
                HttpCookie hc = new HttpCookie("user");
                hc.Values["username"] = user.username;
                hc.Values["pwd"] = user.pwd;
                hc.Values["IsChecked"] = user.IsChecked;
                hc.Expires = DateTime.Now.AddDays(2);//设置cookie过期时间
                Response.Cookies.Add(hc);//添加cookie到集合中
            }
            else
            {
                //获取cookie
                HttpCookie getCookie = Request.Cookies["user"];
                //判断cookie是否为null
                if (getCookie != null)
                {
                    //让cookie过期
                    Response.Cookies["user"].Expires = DateTime.Now.AddHours(-1);
                }
            }
            return Redirect("~/Login/Sucess");
        }
        public ActionResult Sucess()
        {
            return View();
        }
    }
}

页面:


@{
    ViewBag.Title = "Index";
}
@model WebApplication1.Models.userinfo

<link href="~/Content/bootstrap.css" rel="stylesheet" />
<script src="~/Scripts/bootstrap.js"></script>

<div class="row"></div>
<div class="container">
    <form action="~/Login/IsLogin" method="post">
        <div class="col-md-12">
            <div class="col-md-5"></div>
            <div class="col-md-2">
                <div class="form-group">
                    <input type="text" name="username" id="username" value="@Model.username" />
                </div>
                <div></div>
                <div class="form-group">
                    <input type="password" name="pwd" id="pwd" value="@Model.pwd"/>
                </div><div class="checkbox">
                    <label>
                        <input type="checkbox" name="IsChecked" id="IsChecked" @Model.IsChecked > 记住密码
                    </label>
                </div>
                <div class="form-group">
                    <input type="submit" name="sub" id="sub" value="登录" />
                </div>
            </div>
            <div class="col-md-5"></div>
        </div>
    </form>
</div>

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值