开篇先不讲解,如何判断用户是否登陆,我们先来看用户登录的部分代码,账户密码都正确后,先将当前登录的用户名记录下来。
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 }
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 }
.Net Mvc判断用户是否登陆、未登陆跳回登陆页、三种完美解决方案
原文:https://www.cnblogs.com/chenyanbin/p/11397576.html