開篇先不講解,如何判斷用戶是否登陸,我們先來看用戶登錄的部分代碼,賬戶密碼都正確后,先將當前登錄的用戶名記錄下來。
1 public ActionResult ProcessLogin() 2 { 3 try 4 { 5 string user_name = Request["LoginId"]; 6 string user_pwd = Request["LoginPwd"]; 7 UserInfo model = new UserInfo(); 8 model.UName = user_name; 9 model.UPwd = user_pwd; 10 if (bllSession.UserInfo.Select(model).Count > 0) //判斷用戶名密碼是否正確 11 { 12 Session["loginUser"] = user_name; //記錄當前登錄的用戶名 13 return Content("ok"); 14 } 15 else 16 { 17 return Content("用戶名或密碼錯誤!你會登陸嗎?"); 18 } 19 } 20 catch (Exception ex) 21 { 22 throw ex; 23 } 24 }
下面開始演示校驗用戶登錄幾種方式
方式一
在每個頁面執行前判斷當前用戶是否登陸,若登陸才可以進入當前頁面,沒有登陸則跳回首頁,網站頁面少的話,可以在每個頁面上添加此方法,隨着項目模塊越來越多,你還會想怎么復制粘貼嘛?Don't repeat youself!
1 public ActionResult Index() 2 { 3 if (Session["loginUser"] == null) 4 { 5 return RedirectToAction("Index", "UserLogin"); 6 } 7 return View(); 8 }
方式二
全局過濾器中校驗用戶是否登陸
創建一個校驗類(LoginCheckFilterAttribute.cs)
1 using System.Web.Mvc; 2 3 namespace Sam.OA.WEBAPP.Models 4 { 5 /// <summary> 6 /// 校驗用戶是否登陸幫助類 7 /// </summary> 8 public class LoginCheckFilterAttribute: ActionFilterAttribute //注意繼承:ActionFilterAttribute 9 { 10 /// <summary> 11 /// 是否校驗,默認為true 12 /// </summary> 13 public bool IsChecked { get; set; } 14 public override void OnActionExecuted(ActionExecutedContext filterContext) 15 { 16 base.OnActionExecuted(filterContext); 17 //校驗用戶是否已登錄 18 if (IsChecked) 19 { 20 if (filterContext.HttpContext.Session["loginUser"] == null) 21 { 22 filterContext.HttpContext.Response.Redirect("/UserLogin/Index"); 23 } 24 } 25 } 26 } 27 }
在全局過濾器中添加這方法(FilterConfig.cs)
1 using Sam.OA.WEBAPP.Models; 2 using System.Web.Mvc; 3 4 namespace Sam.OA.WEBAPP 5 { 6 public class FilterConfig 7 { 8 public static void RegisterGlobalFilters(GlobalFilterCollection filters) 9 { 10 //filters.Add(new HandleErrorAttribute()); 11 filters.Add(new MyExceptionFilterAttribute()); //自定義的過濾規則 12 13 //校驗用戶是否登陸,默認為校驗 14 filters.Add(new LoginCheckFilterAttribute() { IsChecked=true}); 15 } 16 } 17 }
這樣一來 ,所有的頁面都會校驗用戶是否登陸,可實際中偏偏有些地方是不需要校驗用戶是否登陸的,比如:登陸頁面,此時我們如何解決這個問題呢?我們可以給類打上標簽
用戶登錄控制器(UserLoginController.cs)
1 using Sam.OA.BLLFactory; 2 using Sam.OA.Model.Sam; 3 using Sam.OA.WEBAPP.Models; 4 using System; 5 using System.Web.Mvc; 6 7 namespace Sam.OA.WEBAPP.Controllers 8 { 9 /// <summary> 10 /// 打上標簽,不校驗用戶是否登陸 11 /// </summary> 12 [LoginCheckFilterAttribute(IsChecked =false)] 13 public class UserLoginController : Controller 14 { 15 // GET: UserLogin 16 public ActionResult Index() 17 { 18 return View(); 19 } 20 IBllSession bllSession = BllSessionFactory.GetCurrentBllSession(); 21 /// <summary> 22 /// 處理登陸的表單 23 /// </summary> 24 /// <returns></returns> 25 public ActionResult ProcessLogin() 26 { 27 try 28 { 29 string user_name = Request["LoginId"]; 30 string user_pwd = Request["LoginPwd"]; 31 UserInfo model = new UserInfo(); 32 model.UName = user_name; 33 model.UPwd = user_pwd; 34 if (bllSession.UserInfo.Select(model).Count > 0) //判斷用戶名密碼是否正確 35 { 36 Session["loginUser"] = user_name; 37 return Content("ok"); 38 } 39 else 40 { 41 return Content("用戶名或密碼錯誤!你會登陸嗎?"); 42 } 43 } 44 catch (Exception ex) 45 { 46 throw ex; 47 } 48 } 49 } 50 }
這樣一來問題完美的解決了,不需要校驗用戶是否登陸的地方打上標簽~~~~
方式三
手動創建一個控制器基類(BaseController.cs)
1 using System.Web.Mvc; 2 3 namespace Sam.OA.WEBAPP.Controllers 4 { 5 /// <summary> 6 /// 控制器基類幫助類 7 /// 作者:陳彥斌 8 /// 時間:2019年8月22日23:53:35 9 /// </summary> 10 public class BaseController:Controller 11 { 12 public bool IsCheckedUserLogin = true; 13 protected override void OnActionExecuted(ActionExecutedContext filterContext) 14 { 15 base.OnActionExecuted(filterContext); 16 //校驗用戶是否已登錄 17 if (IsCheckedUserLogin ) 18 { 19 if (filterContext.HttpContext.Session["loginUser"] == null) 20 { 21 filterContext.HttpContext.Response.Redirect("/UserLogin/Index"); 22 } 23 } 24 } 25 } 26 }
此時,我們需要做校驗的控制器全部改寫成繼承控制器基類
1 using Sam.OA.BLLFactory; 2 using Sam.OA.Model.Sam; 3 using System.Web.Mvc; 4 5 namespace Sam.OA.WEBAPP.Controllers 6 { 7 /// <summary> 8 /// 從繼承:Controller改為繼承基類:BaseController 9 /// </summary> 10 public class UserInfoController : BaseController //:Controller 11 { 12 // GET: UserInfo 13 IBllSession bll = BllSessionFactory.GetCurrentBllSession(); 14 public ActionResult Index() 15 { 16 UserInfo model = new UserInfo(); 17 ViewData.Model = bll.UserInfo.Select(model,"1=1"); 18 return View(); 19 } 20 public ActionResult Create() 21 { 22 return View(); 23 } 24 [HttpPost] 25 public ActionResult Create(UserInfo model) 26 { 27 if (ModelState.IsValid) 28 { 29 bll.UserInfo.Add(model); 30 } 31 return RedirectToAction("Index"); 32 } 33 } 34 }
那么問題又來了,有些頁面不校驗如何做呢?要么不繼承基類,要么按照下面方法配置,是不是感覺很靈活嘞
1 using Sam.OA.BLLFactory; 2 using Sam.OA.Model.Sam; 3 using System; 4 using System.Web.Mvc; 5 6 namespace Sam.OA.WEBAPP.Controllers 7 { 8 public class UserLoginController :BaseController //:Controller 9 { 10 public UserLoginController() 11 { 12 this.IsCheckedUserLogin = false; //不校驗用戶是否登陸 13 } 14 // GET: UserLogin 15 public ActionResult Index() 16 { 17 return View(); 18 } 19 IBllSession bllSession = BllSessionFactory.GetCurrentBllSession(); 20 /// <summary> 21 /// 處理登陸的表單 22 /// </summary> 23 /// <returns></returns> 24 public ActionResult ProcessLogin() 25 { 26 try 27 { 28 string user_name = Request["LoginId"]; 29 string user_pwd = Request["LoginPwd"]; 30 UserInfo model = new UserInfo(); 31 model.UName = user_name; 32 model.UPwd = user_pwd; 33 if (bllSession.UserInfo.Select(model).Count > 0) //判斷用戶名密碼是否正確 34 { 35 Session["loginUser"] = user_name; 36 return Content("ok"); 37 } 38 else 39 { 40 return Content("用戶名或密碼錯誤!你會登陸嗎?"); 41 } 42 } 43 catch (Exception ex) 44 { 45 throw ex; 46 } 47 } 48 } 49 }