ASP.net 完整登錄流程
阿新 • • 發佈:2017-10-20
create 資源 設置 behavior min clas 校驗 密碼 string
登錄流程
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using MyProject.IBLL; using MyProject.BLL; using MyProject.Common; using System.Drawing; using System.IO; namespace MyProject.Controllers { public class LoginController : Controller {// // GET: /Login/ IUserInfoService userInfoService = new UserInfoService(); //展示登錄頁面 public ActionResult Index() { if (CheckCookieInfo()) { return Redirect("/UserInfo/Index"); } return View(); } privatebool CheckCookieInfo() { bool flag = false; if (Request.Cookies["cp1"] != null && Request.Cookies["cp2"] != null) { //獲取瀏覽器傳遞過來的Cookie值 string username = Request.Cookies["cp1"].Value; string psd = Request.Cookies["cp2"].Value; string msg; if (userInfoService.CheckUser(username, psd, out msg)) { //設置登錄標識 Session["username"] = username; flag = true; } else { //驗證失敗清空無效Cookie Response.Cookies["cp1"].Expires = DateTime.Now.AddDays(-1); Response.Cookies["cp2"].Expires = DateTime.Now.AddDays(-1); } } return flag; } //獲取驗證碼 public ActionResult CreateCode() { YZMHelper yzm = new YZMHelper(); Session["vCode"] = yzm.Text; MemoryStream ms = new MemoryStream(); yzm.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp); byte[] bytes = ms.ToArray(); ms.Close(); return File(bytes, "Image/JPEG"); } //檢查用戶名是否存在 public ActionResult ChechUserName(string username) { if (userInfoService.LoadEntity(u => u.UserName == username).FirstOrDefault() != null) { return Json(new { res = 1 }, JsonRequestBehavior.AllowGet); } else { return Json(new { res = 0 }, JsonRequestBehavior.AllowGet); } } //登錄校驗 public ActionResult CkeckUser() { string username = Request["UserName"]; string psd = Request["UserPwd"]; string vcode = Request["vCode"]; bool remmberFlag = Request["remmber"]=="1"?true:false; if (vcode.Equals(Session["vCode"].ToString(), StringComparison.CurrentCultureIgnoreCase)) { //清空保存驗證碼Session Session["vCode"] = null; string msg; if (userInfoService.CheckUser(username, psd, out msg)) { Session["username"] = username; if (remmberFlag) { //設置Cokkie信息 HttpCookie cookie1 = new HttpCookie("cp1", username); //密碼最好使用MD5加密 HttpCookie cookie2 = new HttpCookie("cp2", psd); //設置過期時間 cookie1.Expires = DateTime.Now.AddDays(3); cookie2.Expires = DateTime.Now.AddDays(3); Response.Cookies.Add(cookie1); Response.Cookies.Add(cookie2); } return Json(new { res = 1, msg = "/UserInfo/Index" }); } else { //清空保存驗證碼Session, Session["vCode"] = null; return Json(new { res = 0, msg = msg }); } } else { return Json(new { res = 2 }); } } } }
這種方法僅限於資源全儲存於一臺服務器上,如網站分布在多臺服務器上,則需要單獨將登錄標識儲存於數據庫,然後給瀏覽器返回一個隨機序列號作為SessionID,瀏覽器下次訪問時會攜帶這個SessionId,然後從數據庫(Memcache緩存)中查找,根據查找結果判斷用戶是否已經登錄
這是實現服務器端分布式的必要的操作
string sessionId =Guid.NewGuid().ToString();
//作為Memcache的key Common.MemcacheHelper.Set(sessionId,username( 登錄標識,可以隨意設置 ), DateTime.Now.AddMinutes(20));
//使用Memcache代替Session解決數據在不同Web服務器之間共享的問題。 Response.Cookies["sessionId"].Value = sessionId;
//將Memcache的key以cookie的形式返回到瀏覽器端的內存中,當用戶再次請求其它的頁面請求報文中會以Cookie將該值再次發送服務端。
接下來會介紹關於怎麽用Memcache儲存登錄標識
ASP.net 完整登錄流程