C#通过Cookie记住登录信息

MVC前台代码

@{
    ViewBag.Title = "Index";
}
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script type="text/javascript">
    function userLogin() {
        var url = '@Url.Action("UserLogin","Home")';
        var UserName = $('#UserName').val();
        var Password = $('#Password').val();
        var DoRemember = $('#DoRemember').is(':checked');
        $.post(url, { UserName: UserName, Password: Password, DoRemember: DoRemember }, function (result) {
            if (result.toUpperCase() == 'TRUE') {
                alert('登录成功!');
            }
        });
    }
</script>
<h2>Index</h2>
<table>
    <tr>
        <td>用户名</td>
        <td><input type="text" id="UserName" value="@Model.UserName" /></td>
    </tr>
    <tr>
        <td>密码</td>
        <td><input type="password" id="Password" value="@Model.Password" /></td>
    </tr>
    <tr>
        <td>
            <input id="DoRemember" type="checkbox" />记住密码
        </td>
        <td> </td>
    </tr>
    <tr>
        <td><input type="button" value="登录" οnclick="userLogin();" /></td>
        <td> </td>
    </tr>
</table>

MVC后台代码

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

namespace WebApplication100.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            HttpCookie cookie = Request.Cookies["UserInfoRemember"];
            Student Model = new Student();
            if (cookie != null)
            {
                Model.UserName = cookie["UserName"].ToString();
                Model.Password = cookie["Password"].ToString();
            }
            return View(Model);
        }
        /// <summary>
        ///登录
        /// </summary>
        /// <param name="UserName">用户名</param>
        /// <param name="Passwrod">密码</param>
        /// <param name="Remeber">是否记住用户名、密码</param>
        [HttpPost]
        public bool UserLogin(string UserName, string Password, bool DoRemember)
        {
            if (DoRemember)
            {
                HttpCookie cookie = new HttpCookie("UserInfoRemember");
                cookie.HttpOnly = true;
                cookie["UserName"] = UserName;
                cookie["Password"] = Password;
                cookie.Expires = DateTime.MaxValue;
                Response.Cookies.Add(cookie);
            }
            else
            {
                HttpCookie cookie = Request.Cookies["UserInfoRemember"];
                if (cookie != null)
                {
                    cookie.Expires = DateTime.Now.AddDays(-1);//立即过期
                    Response.Cookies.Add(cookie);//重新写入才能使Cookies["userinfo"]失效*/   
                }
            }
            return true;
        }
    }
    public class Student
    {
        public string UserName { get; set; }
        public string Password { get; set; }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值