网页用户登录权限校验的两种实现方式

    网页登录界面进行登录后,如果不进行登录校验,则在其它页面无法知道该用户是否进行了登录。故需要对用户的登录进行校验,这里将介绍两种登录校验的方式。分别为利用自定义行为过滤器进行登录校验,另外一种是构建控制器基类的校验实现子类的登录校验。具体实现如下:
这里以Session的方式进行用户登录信息存储为例进行两种方式的介绍:
  方式一:自定义行为过滤器校验:
第一步:在用户登录成功后,将用户信息添加到Session中:

        //对提交的用户名和密码进行验证
        public ActionResult ProcessLogin()
        {
            #region 验证码
            //1、验证验证码是否正确
            string inputNum = Request["vCode"];
            string sessionNum = Session["VNum"] as string;
            //校验完session后清空session
            Session["VNum"] = null;
            if (string.IsNullOrEmpty(sessionNum))
            {
                return Content("验证码输入错误!");
            }
            if (inputNum != sessionNum)
            {
                return Content("验证码输入错误!");
            }
            #endregion

            #region 登录验证
            string uid = Request["LoginCode"];
            string pwd = Request["LoginPwd"];
            short delNum=(short) DelFlagEnum.Normal;
            UserInfo users= service.GetEntities(u => u.UName == uid && u.Pwd == pwd && u.DelFlag == delNum).FirstOrDefault();
            if (users==null)
            {
                return Content("用户名或密码错误,请重新登录!");
            }

            Session["userLogin"] = users;   //将登录的用户信息存储到Session中
            return Content("OK");
            #endregion
        }


第二步:创建自定义行为过滤器类:LoginCheckFilterAttribute.cs具体代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MyOA.UI.Models
{
    public class LoginCheckFilterAttribute:ActionFilterAttribute
    {
        //为实现该过滤器的类提供设定值,以判定是否需要进行登录验证
        public bool isChecked { get; set; }
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);
            if (isChecked)
            {
                // 校验用户是否登录,如果没有对应的Session存储登录对象,则表示没有进行登录,则直接将页面跳转到登录页面
                if (filterContext.HttpContext.Session["userLogin"] == null)
                {
                    filterContext.HttpContext.Response.Redirect("/UserLogin/Login");
                }
            }

        }
    }
}

第三步:对要进行登录验证的类或者方法添加上述自定义登录行为过滤器

using MyOA.UI.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MyOA.UI.Controllers
{
    public class HomeController : Controller
    {
        [LoginCheckFilter(isChecked=true)]
        public ActionResult Index()
        {
            return View();
        }
    }
}

利用自定义行为过滤器进行登录校验的主要步骤如上。如果某个类或者某个方法不需要实现登录校验,则将isCheck赋值为false即可。

方式二:建立控制器基类实现子类的登录校验
第一步:还是和上面的方法一样,将用户的登录信息存储到Session中;
第二步:构建基类控制器BaseController,并在基类控制器中实现登录校验:

using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MyOA.UI.Controllers
{
    public class BaseController : Controller
    {
        //实现其它地方对登录用户信息的快速调用
        public UserInfo userInfoLogin { get; set; }
        //设定判定是否需要进行登录校验的标志
        public bool isCheckLogin = true;
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);
            if (isCheckLogin)
            {
                if (filterContext.HttpContext.Session["userLogin"] == null)
                {
                    filterContext.HttpContext.Response.Redirect("/UserLogin/Login");
                }
                else
                {
                    userInfoLogin = filterContext.HttpContext.Session["userLogin"] as UserInfo;
                }
            }
        }

    }
}

第三步:将需要进行登录校验的类继承自 BaseController基类控制器:

using MyOA.UI.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MyOA.UI.Controllers
{
    public class HomeController : BaseController
    {
        public ActionResult Index()
        {
            return View();
        }
    }
}



  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值