mvc记住密码功能

        public ActionResult Index()

        {
            User users = new User();

          //判断当前cookie是否存在用户名和密码,若存在则传给前台,直接赋值

            HttpCookie cookie = System.Web.HttpContext.Current.Request.Cookies.Get("COOKIE_NAME_FOR_USER");
            users.Name = (cookie == null ? "" : cookie["COOKIE_USER_NAME"].ToString().Trim());
            users.Password = (cookie == null ? "" : cookie["COOKIE_USER_PASS"].ToString().Trim());
            if (cookie != null)
            {
                ViewData["checked"] = "checked";
            }
            else
            {
                ViewData["checked"] = "";
            }
            return View(users);
        }
        public ActionResult Check()
        {
            string  messge;
            var     remeber = Request.Form["Isre"];
            string LoginName = Request.Form["name"];
            string Password = Request.Form["password"];
            var user = (from u1 in db.Users
                       where u1.UserName == LoginName && u1.Password == Password
                        select u1).FirstOrDefault();
            if (remeber == "true")
            {

//如果选中记住密码则把用户名和密码存入cookie否则存为空

                HttpCookie cookie = new HttpCookie("COOKIE_NAME_FOR_USER");

                cookie.Expires = DateTime.Now.AddYears(1);
                cookie["COOKIE_USER_NAME"] = LoginName;
                cookie["COOKIE_USER_PASS"] = Password;
                System.Web.HttpContext.Current.Response.Cookies.Add(cookie);
            }
            else
            {
                HttpCookie cookie = new HttpCookie("COOKIE_NAME_FOR_USER");
                cookie.Expires = DateTime.Now.AddYears(-1);
                Request.Cookies.Add(cookie);
                cookie["COOKIE_USER_NAME"] = null;
                cookie["COOKIE_USER_PASS"] = null;
                System.Web.HttpContext.Current.Response.Cookies.Add(cookie);
            }
            if (user == null)
            {
                messge = "error";
            }
            else
            {
                messge = "ok";
                HttpContext.Session["Name"] = LoginName;
            }
            return Content(messge);
        }

前台页面:

@model 沃德OA管理系统.Models.User
@{
    Layout = null;
}
<script type="text/javascript">
    function check() {
        var uer = $("#ipt_loginname").val();
        var pwd = $("#ipt_password").val();
        var Isre = $("input[type='checkbox']").is(':checked')
            if (uer == "") {
                $("#error").html("用户名不能为空!!");
                return;
            }
            if (pwd == "") {
                $("#error").html("密码不能为空!!");
                return;
            }
         
            $.ajax({
                url: "/Login/Check",
                type: 'POST',
                dataType: 'text',
                async:true,
                data: { name: uer, password: pwd, Isre: Isre, },
                error :function(strValue){
                    $("#error").html("请求失败!!");
                },
                success: function (strValue) {
                    if (strValue.trim() == "ok") {
                        window.location="/Home/Index";
                    }
                    else {
                        $("#error").html("用户名或密码错误!");
                    }
                }
            
            });
        };
</script>
<style type="text/css">
</style>
<body>
    <div style="background-image: url('../../Content/Image/u=0.jpg'); background-repeat: no-repeat; background-size: 100% 100%; color: white; height: 100%; left: 0; position: absolute; top: 0; width: 100%;">
        <form method="post" id="loginform">
            <div class="login" style="left: 50%; margin: 20% 0 20px -150px; position: relative;">
                <p style="margin-top: 10px;">
                    <label for="ipt_loginname">登录账号</label>
                    <input id="ipt_loginname" name="LoginName" placeholder="请输入账号" type="text" value="@Model.Name"/>
                </p>
                <p>
                    <label for="ipt_password">登录密码</label>
                    <input id="ipt_password" name="Password" placeholder="请输入密码" type="password" value="@Model.Password" />
                </p>
               
                <div style="height:15px"></div>
                <div style="text-align:right;margin-right:80px">
                    <input type="button" οnclick="check()" id="post" value="登录" />
                    <input  id="Isre" name="Isre" type="checkbox"   @ViewData["checked"] /><span style="font-size: 12px;">记住密码</span>
                </div>
                <p style="color: yellow; margin-left:100px" id="error"></p>
            </div>
        </form>
        </div>
</body>

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

这个月太忙没时间看C++

你的鼓励将是我创作的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值