开篇先不讲解, 如何判断用户是否登陆, 我们先来看用户登录的部分代码, 账户密码都正确后, 先将当前登录的用户名记录下来.
- public ActionResult ProcessLogin()
- {
- try
- {
- string user_name = Request["LoginId"];
- string user_pwd = Request["LoginPwd"];
- UserInfo model = new UserInfo();
- model.UName = user_name;
- model.UPwd = user_pwd;
- if (bllSession.UserInfo.Select(model).Count> 0) // 判断用户名密码是否正确
- {
- Session["loginUser"] = user_name; // 记录当前登录的用户名
- return Content("ok");
- }
- else
- {
- return Content("用户名或密码错误! 你会登陆吗?");
- }
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
下面开始演示校验用户登录几种方式
方式一
在每个页面执行前判断当前用户是否登陆, 若登陆才可以进入当前页面, 没有登陆则跳回首页, 网站页面少的话, 可以在每个页面上添加此方法, 随着项目模块越来越多, 你还会想怎么复制粘贴嘛? Don't repeat youself!
- public ActionResult Index()
- {
- if (Session["loginUser"] == null)
- {
- return RedirectToAction("Index", "UserLogin");
- }
- return View();
- }
方式二
全局过滤器中校验用户是否登陆
创建一个校验类 (LoginCheckFilterAttribute.cs)
- using System.web.Mvc;
- namespace Sam.OA.WEBAPP.Models
- {
- /// <summary>
- /// 校验用户是否登陆帮助类
- /// </summary>
- public class LoginCheckFilterAttribute: ActionFilterAttribute // 注意继承: ActionFilterAttribute
- {
- /// <summary>
- /// 是否校验, 默认为 true
- /// </summary>
- public bool IsChecked { get; set; }
- public override void OnActionExecuted(ActionExecutedContext filterContext)
- {
- base.OnActionExecuted(filterContext);
- // 校验用户是否已登录
- if (IsChecked)
- {
- if (filterContext.HttpContext.Session["loginUser"] == null)
- {
- filterContext.HttpContext.Response.Redirect("/UserLogin/Index");
- }
- }
- }
- }
- }
在全局过滤器中添加这方法 (FilterConfig.cs)
- using Sam.OA.WEBAPP.Models;
- using System.Web.Mvc;
- namespace Sam.OA.WEBAPP
- {
- public class FilterConfig
- {
- public static void RegisterGlobalFilters(GlobalFilterCollection filters)
- {
- //filters.Add(new HandleErrorAttribute());
- filters.Add(new MyExceptionFilterAttribute()); // 自定义的过滤规则
- // 校验用户是否登陆, 默认为校验
- filters.Add(new LoginCheckFilterAttribute() { IsChecked=true});
- }
- }
- }
这样一来 , 所有的页面都会校验用户是否登陆, 可实际中偏偏有些地方是不需要校验用户是否登陆的, 比如: 登陆页面, 此时我们如何解决这个问题呢? 我们可以给类打上标签
用户登录控制器 (UserLoginController.cs)
- using Sam.OA.BLLFactory;
- using Sam.OA.Model.Sam;
- using Sam.OA.WEBAPP.Models;
- using System;
- using System.Web.Mvc;
- namespace Sam.OA.WEBAPP.Controllers
- {
- /// <summary>
- /// 打上标签, 不校验用户是否登陆
- /// </summary>
- [LoginCheckFilterAttribute(IsChecked =false)]
- public class UserLoginController : Controller
- {
- // GET: UserLogin
- public ActionResult Index()
- {
- return View();
- }
- IBllSession bllSession = BllSessionFactory.GetCurrentBllSession();
- /// <summary>
- /// 处理登陆的表单
- /// </summary>
- /// <returns></returns>
- public ActionResult ProcessLogin()
- {
- try
- {
- string user_name = Request["LoginId"];
- string user_pwd = Request["LoginPwd"];
- UserInfo model = new UserInfo();
- model.UName = user_name;
- model.UPwd = user_pwd;
- if (bllSession.UserInfo.Select(model).Count> 0) // 判断用户名密码是否正确
- {
- Session["loginUser"] = user_name;
- return Content("ok");
- }
- else
- {
- return Content("用户名或密码错误! 你会登陆吗?");
- }
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- }
- }
这样一来问题完美的解决了, 不需要校验用户是否登陆的地方打上标签~~~~
方式三
手动创建一个控制器基类 (BaseController.cs)
- using System.Web.Mvc;
- namespace Sam.OA.WEBAPP.Controllers
- {
- /// <summary>
- /// 控制器基类帮助类
- /// 作者: 陈彦斌
- /// 时间: 2019 年 8 月 22 日 23:53:35
- /// </summary>
- public class BaseController:Controller
- {
- public bool IsCheckedUserLogin = true;
- protected override void OnActionExecuted(ActionExecutedContext filterContext)
- {
- base.OnActionExecuted(filterContext);
- // 校验用户是否已登录
- if (IsCheckedUserLogin )
- {
- if (filterContext.HttpContext.Session["loginUser"] == null)
- {
- filterContext.HttpContext.Response.Redirect("/UserLogin/Index");
- }
- }
- }
- }
- }
此时, 我们需要做校验的控制器全部改写成继承控制器基类
- using Sam.OA.BLLFactory;
- using Sam.OA.Model.Sam;
- using System.Web.Mvc;
- namespace Sam.OA.WEBAPP.Controllers
- {
- /// <summary>
- /// 从继承: Controller 改为继承基类: BaseController
- /// </summary>
- public class UserInfoController : BaseController //:Controller
- {
- // GET: UserInfo
- IBllSession bll = BllSessionFactory.GetCurrentBllSession();
- public ActionResult Index()
- {
- UserInfo model = new UserInfo();
- ViewData.Model = bll.UserInfo.Select(model,"1=1");
- return View();
- }
- public ActionResult Create()
- {
- return View();
- }
- [HttpPost]
- public ActionResult Create(UserInfo model)
- {
- if (ModelState.IsValid)
- {
- bll.UserInfo.Add(model);
- }
- return RedirectToAction("Index");
- }
- }
- }
那么问题又来了, 有些页面不校验如何做呢? 要么不继承基类, 要么按照下面方法配置, 是不是感觉很灵活嘞
- using Sam.OA.BLLFactory;
- using Sam.OA.Model.Sam;
- using System;
- using System.Web.Mvc;
- namespace Sam.OA.WEBAPP.Controllers
- {
- public class UserLoginController :BaseController //:Controller
- {
- public UserLoginController()
- {
- this.IsCheckedUserLogin = false; // 不校验用户是否登陆
- }
- // GET: UserLogin
- public ActionResult Index()
- {
- return View();
- }
- IBllSession bllSession = BllSessionFactory.GetCurrentBllSession();
- /// <summary>
- /// 处理登陆的表单
- /// </summary>
- /// <returns></returns>
- public ActionResult ProcessLogin()
- {
- try
- {
- string user_name = Request["LoginId"];
- string user_pwd = Request["LoginPwd"];
- UserInfo model = new UserInfo();
- model.UName = user_name;
- model.UPwd = user_pwd;
- if (bllSession.UserInfo.Select(model).Count> 0) // 判断用户名密码是否正确
- {
- Session["loginUser"] = user_name;
- return Content("ok");
- }
- else
- {
- return Content("用户名或密码错误! 你会登陆吗?");
- }
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- }
- }
以上所有问题都已经完美解决~
来源: https://www.cnblogs.com/chenyanbin/p/11397576.html