后台记住密码
public class CookieHelper { /// <summary> /// 清除指定Cookie /// </summary> /// <param name="cookiename">cookiename</param> public static void ClearCookie(string cookiename) { HttpCookie cookie = HttpContext.Current.Request.Cookies[cookiename]; if (cookie != null) { cookie.Expires = DateTime.Now.AddYears(-3); HttpContext.Current.Response.Cookies.Add(cookie); } } /// <summary> /// 获取指定Cookie值 /// </summary> /// <param name="cookiename">cookiename</param> /// <returns></returns> public static string GetCookieValue(string cookiename) { HttpCookie cookie = HttpContext.Current.Request.Cookies[cookiename]; string cookievalue = string.Empty; if (cookie != null) { cookievalue = cookie.Value; } return cookievalue; } /// <summary> /// 添加一个Cookie /// </summary> /// <param name="cookiename">cookie名</param> /// <param name="cookievalue">cookie值</param> /// <param name="expires">过期时间 DateTime</param> public static void SetCookie(string cookiename, string cookievalue, DateTime expires) { HttpCookie cookie = new HttpCookie(cookiename) { Value = cookievalue, Expires = expires }; HttpContext.Current.Response.Cookies.Add(cookie); } }
托这个帮助类
然后这是控制器的
public ActionResult Index() { //获取cookie保存的账号密码 string UserNameCookie = CookieHelper.GetCookieValue("UserNameCookie"); UserNameCookie = HttpUtility.UrlDecode(UserNameCookie); string UserPasswordCookie = CookieHelper.GetCookieValue("UserPasswordCookie"); UserPasswordCookie = HttpUtility.UrlDecode(UserPasswordCookie); ViewBag.UserName = UserNameCookie; ViewBag.UserPassWord = UserPasswordCookie; return View(); } public int Login(Users model) { string json = client.Post("Post", JsonConvert.SerializeObject(model)); int n = json == "" ? 0 : Convert.ToInt32(json); if (n>0) { string UserNameCookie = CookieHelper.GetCookieValue("UserNameCookie"); string UserPasswordCookie = CookieHelper.GetCookieValue("UserPasswordCookie"); Session["UserName"] = model.UserName; //判断是否把登陆账号密码到cookie if (model.IsSaveLoInfo) { string UserName = HttpUtility.UrlEncode(model.UserName); string UserPassWord = HttpUtility.UrlEncode(model.UserPassWord); if (UserNameCookie != UserName)//当输入的用户名与cookie保存的不同,修改cookie的 CookieHelper.SetCookie("UserNameCookie", UserName, DateTime.MaxValue); if (UserPasswordCookie != UserPassWord) CookieHelper.SetCookie("UserPasswordCookie", UserPassWord, DateTime.MaxValue); } else { CookieHelper.ClearCookie("UserNameCookie"); CookieHelper.ClearCookie("UserNameCookie"); } return 1; } else { return 0; } }
其中我们需要进行一次编码和解码
原因是文字会变成乱码
我们把用户名编码
然后再显示是再解码
问题就解决了
然后这是视图的
<div class="content"> <div class="bidTitle">管理员登录</div> <div class="logCon"> <div class="line"> <span>账号:</span> <input class="bt_input" type="text" name="UserName" /> </div> <div class="line"> <span>密码:</span> <input class="bt_input" type="password" name="UserPassWord" /> </div> <div class="line"> <input id="rd" type="checkbox" name="rd_S" style="margin: 20px 0px 2px 20px" /> <label style="margin-top: 20px">记住账号密码</label> </div> <button type="button" class="logingBut">登录</button> </div> </div> <div id="slideBar"></div> <div style="text-align:center;"> </div> <script> var rootPath = '@Url.Content("~/Admin/")'; var username = '@ViewBag.UserName'; var password = '@ViewBag.UserPassWord'; $(document).ready(function () { if (username != "") { $("[name=UserName]").val(username); $("[name=UserPassWord]").val(password); $("#rd").attr("checked", "checked"); //当cookie记录了账号密码,则初始化‘记住账号密码’复选框时为勾选状态 } else { $("#rd").removeAttr("checked"); } }); //登录 $(".logingBut").click(function () { var IsSaveLoInfo; //标识是否记住账号密码 if ($("#rd").prop("checked")) { IsSaveLoInfo = true; } var obj = { UserName: $("[name=UserName]").val(), UserPassWord: $("[name=UserPassWord]").val(), IsSaveLoInfo:IsSaveLoInfo }; $.ajax({ url: "/Users/Login", loading: "正在登陆中...", data: obj, success: function (d) { if (d > 0) { location.href = "/Users/HomePage"; } else { alert('登录失败'); } } }) }) </script>
总结
我们通过后台的Cookie来保存起用户名和密码
然后通过编码和解码来保证了用户名和密码的准确性
最后我们前端来获取其值就可以了

浙公网安备 33010602011771号