登录_退出
Posted liusheng11188
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了登录_退出相关的知识,希望对你有一定的参考价值。
客户端代码_包含记住我 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Login.aspx.cs" Inherits="CZBK.TestProject.WebApp._2014_11_13.Login" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=GBK"><title> 网站管理后台登录 </title><link href="Image/style.css" rel="stylesheet" type="text/css"> <style type="text/css"> <!-- body { margin-top: 150px; } --> </style> <script type="text/javascript"> window.onload = function () { var url = document.getElementById("url"); url.onclick = function () { var image = document.getElementById("Image1"); image.src = "ValidateCode.ashx?d="+ new Date().getMilliseconds(); } } </script> </head> <body> <form name="form1" runat="server"> <div> <table width="549" height="287" border="0" align="center" cellpadding="0" cellspacing="0" background="Image/login_bg.jpg"> <tbody><tr> <td width="23"><img src="Image/login_leftbg.jpg" width="23" height="287"></td> <td width="503" valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0"> <tbody><tr> <td width="49%" valign="bottom"><table width="90%" border="0" align="center" cellpadding="0" cellspacing="0"> <tbody><tr> <td height="100" valign="top" class="login_text"><div align="left"> 网站后台管理系统</div></td> </tr> <tr> <td><div align="right"><img src="Image/login_img.jpg" width="104" height="113"></div></td> </tr> </tbody></table></td> <td width="2%"><img src="Image/login_line.jpg" width="6" height="287"></td> <td width="49%"><div align="right"> <table width="223" border="0" cellspacing="0" cellpadding="0"> <tbody><tr> <td><img src="Image/login_tit.jpg" width="223" height="30"></td> </tr> <tr> <td><table width="100%" border="0" cellspacing="10" cellpadding="0"> <tbody><tr> <td width="28%"><div align="left">用户名:</div></td> <td width="72%"><div align="left"><span class="style1"> <input name="txtClientID" type="text" id="txtClientID" class="form2" style="height:15px;width:140px;"> </span></div></td> </tr> <tr> <td><div align="left">密 码:</div></td> <td><div align="left"><span class="style1"> <input name="txtPassword" type="password" id="txtPassword" class="form2" style="height:15px;width:140px;"></span></div></td> </tr> <tr> <td><div align="left">验证码:</div></td> <td><div align="left"> <a href="javascript:void(0)" id="url"><img id="Image1" src="ValidateCode.ashx" style="border-width:0px;"/></a> </div></td> </tr> <tr> <td><div align="left">验证码:</div></td> <td><div align="left"><span class="style1"> <input name="txtCode" type="text" size="8" id="txtCode" class="form2" style="height:15px;"><%=ErrorMsg %></span></div></td> </tr> </tbody></table></td> </tr> <tr> <td align="center"><table width="100%" border="0" cellspacing="0" cellpadding="0"> <tbody><tr> <td><div align="center"><a href="http://www.800kb.com/ClientManager/#"></a></div></td> <td><div align="center"> 记住我<input type="checkbox" name="CheckMe" value="1" /> <input type="image" name="btnLogin" id="btnLogin" src="Image/login_menu2.jpg" style="border-width:0px;"><a href="http://www.800kb.com/ClientManager/#"></a></div></td> </tr> </tbody></table> <strong><span style="color: #3180b7">辽ICP备10012593号</span></strong></td> </tr> </tbody></table> </div></td> </tr> </tbody></table></td> <td width="23"><img src="Image/login_rigbg.jpg" width="23" height="287"></td> </tr> </tbody></table> </div> </form> </body></html>
1 服务器端代码 2 public partial class Login : System.Web.UI.Page 3 { 4 BLL.UserInfoService UserInfoService = new BLL.UserInfoService(); 5 public string ErrorMsg { get; set; } 6 protected void Page_Load(object sender, EventArgs e) 7 { 8 //服务端也要校验.(与客户端校验自己完成) 9 //1.判断验证码是否正确 10 if (IsPostBack)//表示用户单击了登录按钮 11 { 12 if (CheckValidateCode()) 13 { 14 UserLoginIn(); 15 } 16 else 17 { 18 ErrorMsg = "验证码错误!!"; 19 } 20 } 21 else//表示GET请求 22 { 23 CheckCookieInfo();//校验Cookie信息 24 } 25 26 } 27 protected void CheckCookieInfo() 28 { 29 //判断Cookie中是否有值 30 if (Request.Cookies["cp1"] != null && Request.Cookies["cp2"] != null) 31 { 32 string userName = Request.Cookies["cp1"].Value; 33 string userPwd = Request.Cookies["cp2"].Value; 34 //判断Cookie中存储的用户名是否正确 35 UserInfo userInfo=UserInfoService.GetModel(userName); 36 if (userInfo != null) 37 { 38 //判断密码是否正确. 39 if (userPwd == Common.WebCommon.GetMd5String(Common.WebCommon.GetMd5String(userInfo.UserPass))) 40 { 41 Session["userInfo"] = userInfo; 42 Response.Redirect("AdminIndex.aspx"); 43 } 44 } 45 Response.Cookies["cp1"].Expires = DateTime.Now.AddDays(-1); 46 Response.Cookies["cp2"].Expires = DateTime.Now.AddDays(-1); 47 } 48 } 49 protected void UserLoginIn() 50 { 51 string userName = Request.Form["txtClientID"]; 52 string userPwd = Request.Form["txtPassword"]; 53 string msg = string.Empty; 54 UserInfo userInfo=null; 55 bool b= UserInfoService.CheckUserInfo(userName, userPwd,out msg,out userInfo); 56 if (b) 57 { 58 Session["userInfo"] = userInfo; 59 //判断用户是否选择了记住我 60 if (!string.IsNullOrEmpty(Request.Form["CheckMe"])) 61 { 62 HttpCookie cookie1 = new HttpCookie("cp1",userName); 63 HttpCookie cookie2 = new HttpCookie("cp2", Common.WebCommon.GetMd5String(Common.WebCommon.GetMd5String(userPwd))); 64 cookie1.Expires = DateTime.Now.AddDays(3); 65 cookie2.Expires = DateTime.Now.AddDays(3); 66 Response.Cookies.Add(cookie1); 67 Response.Cookies.Add(cookie2); 68 } 69 Response.Redirect("AdminIndex.aspx"); 70 } 71 else 72 { 73 ErrorMsg = msg; 74 } 75 } 76 /// <summary> 77 /// 对验证码进行校验 78 /// </summary> 79 /// <returns></returns> 80 protected bool CheckValidateCode() 81 { 82 bool isSuccess = false; 83 if (Session["code"] != null) 84 { 85 string sysCode = Session["code"].ToString(); 86 string txtCode = Request.Form["txtCode"]; 87 if (sysCode.Equals(txtCode, StringComparison.InvariantCultureIgnoreCase)) 88 { 89 Session["code"] = null; 90 // Session.Remove("code"); 91 isSuccess = true; 92 } 93 } 94 return isSuccess; 95 96 } 97 }
验证码处理要增加ValidateCode
退出登录
1 public void ProcessRequest(HttpContext context) 2 { 3 context.Response.ContentType = "text/plain"; 4 if (context.Session["userInfo"] != null) 5 { 6 context.Session["userInfo"] = null; 7 context.Response.Cookies["cp1"].Expires = DateTime.Now.AddDays(-1); 8 context.Response.Cookies["cp2"].Expires = DateTime.Now.AddDays(-1); 9 context.Response.Redirect("Login.aspx"); 10 } 11 12 }
1 public class WebCommon 2 { 3 /// <summary> 4 /// 对传递过来的字符串进行MD5运算 5 /// </summary> 6 /// <param name="str"></param> 7 /// <returns></returns> 8 public static string GetMd5String(string str) 9 { 10 MD5 md5 = MD5.Create(); 11 byte[]buffer=System.Text.Encoding.UTF8.GetBytes(str); 12 byte[]md5Buffer= md5.ComputeHash(buffer); 13 StringBuilder sb = new StringBuilder(); 14 foreach (byte b in md5Buffer) 15 { 16 sb.Append(b.ToString("x2")); 17 } 18 return sb.ToString(); 19 } 20 }
这样验证码要使用
11.13
以上是关于登录_退出的主要内容,如果未能解决你的问题,请参考以下文章
PHP最彻底的退出登录,清除sessioncookie的代码
进销存系统_SpringSecurity权限框架&登录&用户密码加密&退出