.net登录的一般模式
2018-06-17 22:23:20来源:未知 阅读 ()
思路:
1、检查登录:
private string CheckLogin(HttpContext context) { Model.TUser model = (Model.TUser)context.Session["USERModel"]; if (model == null) { return "{\"msg\": \"0\", \"msgbox\": \"当前为退出\"}"; } else { return "{\"msg\": \"1\", \"msgbox\": \"当前为登录\"}"; } }
2、点击登录:
private string Login(HttpContext context) { BLL.TUser _blluser = new BLL.TUser(); string loginname = context.Request.Params["loginname"]; string password = context.Request.Params["password"]; string issave = context.Request.Params["issave"];//是否保存密码 Model.TUser model_user = _blluser.GetModel("UserLogin='" + loginname + "'"); bool isSuccess = false; string cookiesPassword = ""; //判断 Cookies 是否存在 if (HttpContext.Current.Request.Cookies["password"] != null) { cookiesPassword = HttpContext.Current.Request.Cookies["password"].Value; if (password == cookiesPassword) /// 输入 和 Cookies 密码 一样 { //登录成功 isSuccess = true; } } //判断输入的密码和数据库的是否匹配 if (!isSuccess) { if (password == model_user.PassWord) { //登录成功 isSuccess = true; } else { //登录失败 isSuccess = false; } } if (isSuccess) { context.Session["USERModel"] = model_user; } else { //登录失败 密码不对 return "{\"msg\": \"0\", \"msgbox\": \"密码错误\"}"; } //要保存,则写入cookie if (issave == "true") { HttpCookie cookieLogin = new HttpCookie("loginname"); cookieLogin.Expires = DateTime.Now.AddDays(10); cookieLogin.Value = model_user.UserLogin; HttpContext.Current.Response.Cookies.Add(cookieLogin); HttpCookie cookiePassword = new HttpCookie("password"); cookiePassword.Expires = DateTime.Now.AddDays(10); cookiePassword.Value = password; HttpContext.Current.Response.Cookies.Add(cookiePassword); } else {//设置cookie过期,删除cookie HttpCookie cookieLogin = new HttpCookie("loginname"); cookieLogin.Expires = DateTime.Now.AddDays(-1); HttpContext.Current.Response.Cookies.Add(cookieLogin); HttpCookie cookiePassword = new HttpCookie("password"); cookiePassword.Expires = DateTime.Now.AddDays(-1); HttpContext.Current.Response.Cookies.Add(cookiePassword); } return "{\"msg\": \"1\", \"msgbox\": \"登录成功\"}"; }
3、页面刷新时获取用户保存的账户名和密码并免登陆
private string IsSaveUserLogin(HttpContext context) { ///判断 Cookies 是否存在 if (HttpContext.Current.Request.Cookies["loginname"] == null || HttpContext.Current.Request.Cookies["password"] == null) { return "{\"msg\":-1, \"msgbox\": \"帐号没保存\", \"loginname\": \"\", \"password\": \"\"}"; } else { string loginname = HttpContext.Current.Request.Cookies["loginname"].Value; string password = HttpContext.Current.Request.Cookies["password"].Value; //免登陆 CookieLogin(context, loginname, password); return "{\"msg\":1, \"msgbox\": \"帐号保存过\", \"loginname\": \"" + loginname + "\", \"password\": \"" + password + "\"}"; } } private void CookieLogin(HttpContext context, string cookiesLoginname, string cookiesPassword) { Model.TUser model_user = new Model.TUser(); BLL.TUser _blluser = new BLL.TUser(); bool isSuccess = false; ///判断 Cookies 是否存在 if (!string.IsNullOrEmpty(cookiesLoginname) && !string.IsNullOrEmpty(cookiesPassword)) { model_user = _blluser.GetModel("UserLogin='" + cookiesLoginname + "'"); if (model_user.PassWord == cookiesPassword) /// 数据库 和 Cookies密码 一样 { //登录成功 isSuccess = true; } } if (isSuccess) { //登录成功 context.Session["USERModel"] = model_user; } else { //登录失败 密码不对 //return "{\"msg\": \"0\", \"msgbox\": \"密码错误\"}"; } }
4、用户退出-清除cookie和session
private string UserExit(HttpContext context) { //清除cookie //HttpContext.Current.Request.Cookies.Clear();//无效 HttpCookie cookieLogin = new HttpCookie("loginname"); cookieLogin.Expires = DateTime.Now.AddDays(-1); HttpContext.Current.Response.Cookies.Add(cookieLogin); HttpCookie cookiePassword = new HttpCookie("password"); cookiePassword.Expires = DateTime.Now.AddDays(-1); HttpContext.Current.Response.Cookies.Add(cookiePassword); context.Session["USERModel"] = null; context.Session.Clear(); return ""; }
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- Django项目中使用qq第三方登录。 2019-08-13
- python day2-爬虫实现github登录 2019-08-13
- python爬虫学习之淘宝模拟登录 2019-08-13
- python第一天---我要入个门 2019-07-24
- 利用Python模拟登录pastebin.com 2019-07-24
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash