C#通过Cookie记住登录信息
来源:互联网 发布:淘宝三个评价 编辑:程序博客网 时间:2024/05/16 15:35
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="登录" onclick="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; } }}
1 0
- C#通过Cookie记住登录信息
- cookie记住用户登录信息
- 系统登录后设置Cookie记住登陆信息
- 通过jquery.cookie.js实现记住用户名、密码登录功能
- Cookie登录记住用户名
- c# 实现 通过写入注册表 记住 winform程序 登录名
- Cookie实现登录记住密码
- 通过cookie保存并读取用户登录信息实例
- 通过Cookie保存并读取用户登录信息
- 通过Cookie存放用户登录信息以及安全性问题
- 通过cookie保存并读取用户登录信息
- nodeJs session记住登录信息
- 登录页面使用Cookie 记住密码
- php使用cookie完成登录记住密码
- jsp Cookie记住用户的登录状态
- 登录页面,用cookie实现 记住用户名
- Cookie实现记住用户登录名
- cookie:记住用户登录时的用户名
- 用Visual C++开发数据库应用程序(一)
- Mac 让 iTerm2 记住用户名密码 expect 脚本
- Android控件之WebView ScrollView问题录
- kafka consumer
- <body onload="showAccount();">
- C#通过Cookie记住登录信息
- 记录
- jQuery中$.proxy()的原理和使用
- 使用JSONP解决Ajax跨域访问问题
- Android SDK Manager无法更新列表
- UIView如何管理它的子视图
- HttpResponseMessage && IHttpActionResult
- 公用分页模块之knockout
- 让 UIAlertController 兼容 iOS7