C#通过Cookie记住登录信息

MVC前台代码

  1. @{
  2. ViewBag.Title = "Index";
  3. }
  4. <script src="~/Scripts/jquery-1.10.2.min.js"> </script>
  5. <script type="text/javascript">
  6. function userLogin() {
  7. var url = '@Url.Action("UserLogin","Home")';
  8. var UserName = $( '#UserName').val();
  9. var Password = $( '#Password').val();
  10. var DoRemember = $( '#DoRemember').is( ':checked');
  11. $.post(url, { UserName: UserName, Password: Password, DoRemember: DoRemember }, function (result) {
  12. if (result.toUpperCase() == 'TRUE') {
  13. alert( '登录成功!');
  14. }
  15. });
  16. }
  17. </script>
  18. <h2>Index </h2>
  19. <table>
  20. <tr>
  21. <td>用户名 </td>
  22. <td> <input type="text" id="UserName" value="@Model.UserName" /> </td>
  23. </tr>
  24. <tr>
  25. <td>密码 </td>
  26. <td> <input type="password" id="Password" value="@Model.Password" /> </td>
  27. </tr>
  28. <tr>
  29. <td>
  30. <input id="DoRemember" type="checkbox" />记住密码
  31. </td>
  32. <td>  </td>
  33. </tr>
  34. <tr>
  35. <td> <input type="button" value="登录" onclick="userLogin();" /> </td>
  36. <td>  </td>
  37. </tr>
  38. </table>

MVC后台代码
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. namespace WebApplication100.Controllers
  7. {
  8. public class HomeController : Controller
  9. {
  10. public ActionResult Index()
  11. {
  12. HttpCookie cookie = Request.Cookies[ "UserInfoRemember"];
  13. Student Model = new Student();
  14. if (cookie != null)
  15. {
  16. Model.UserName = cookie[ "UserName"].ToString();
  17. Model.Password = cookie[ "Password"].ToString();
  18. }
  19. return View(Model);
  20. }
  21. /// <summary>
  22. ///登录
  23. /// </summary>
  24. /// <param name="UserName">用户名</param>
  25. /// <param name="Passwrod">密码</param>
  26. /// <param name="Remeber">是否记住用户名、密码</param>
  27. [ HttpPost]
  28. public bool UserLogin(string UserName, string Password, bool DoRemember)
  29. {
  30. if (DoRemember)
  31. {
  32. HttpCookie cookie = new HttpCookie( "UserInfoRemember");
  33. cookie.HttpOnly = true;
  34. cookie[ "UserName"] = UserName;
  35. cookie[ "Password"] = Password;
  36. cookie.Expires = DateTime.MaxValue;
  37. Response.Cookies.Add(cookie);
  38. }
  39. else
  40. {
  41. HttpCookie cookie = Request.Cookies[ "UserInfoRemember"];
  42. if (cookie != null)
  43. {
  44. cookie.Expires = DateTime.Now.AddDays( -1); //立即过期
  45. Response.Cookies.Add(cookie); //重新写入才能使Cookies["userinfo"]失效*/
  46. }
  47. }
  48. return true;
  49. }
  50. }
  51. public class Student
  52. {
  53. public string UserName { get; set; }
  54. public string Password { get; set; }
  55. }
  56. }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值