您的位置:首页 > 其它

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

2017-09-24 21:45 399 查看
    网页登录界面进行登录后,如果不进行登录校验,则在其它页面无法知道该用户是否进行了登录。故需要对用户的登录进行校验,这里将介绍两种登录校验的方式。分别为利用自定义行为过滤器进行登录校验,另外一种是构建控制器基类的校验实现子类的登录校验。具体实现如下:
这里以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();
        }
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: