ASP.NET MVC 实现统一登录验证

1.先写个主页面

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

namespace WebApplication4.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            string name = "";
            //Session里面有值,就表示已经登录
            if (Session["User"]!= null)
            {
                name = Session["User"].ToString();
            }
            //Session=null就跳转到登录页面
            else
            {
                return RedirectToAction("Login", "Login");
            }
            return Content("欢迎【"+name+"】登录");
        }      
    }
}

2.登录页面

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

namespace WebApplication4.Controllers
{
    //登录页加上此特性,不需要做登录验证,要不加会陷入死循环,导致浏览器崩溃
    [SkipCheckLoginAttribute]
    public class LoginController : Controller
    {
        // GET: Login
        [HttpGet]
        public ActionResult Login()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Login(FormCollection forms)
        {
            string name = forms["name"];
            string pwd = forms["pwd"];

            if(name=="Admin" && pwd == "Admin")
            {
                Session["User"] = name;
            }

            return RedirectToAction("Index","Home");
        }
    }
}

View页的登录


@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Login</title>
</head>
<body>
    <div>
        @using (Html.BeginForm("Login", "Login", FormMethod.Post))
        {
            <input type="text" name="name" /><br />
            <input type="text" name="pwd" /><br />
            <input type="submit" value="登录" />
        }
    </div>
</body>
</html>

3.一个登录验证的过滤器CheckLoginAttribute

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

namespace WebApplication4.Filters
{
    using System.Web.Mvc;
    /// <summary>
    /// 统一登陆验证
    /// </summary>
    public class CheckLoginAttribute:ActionFilterAttribute
    {
        //重写ActionFilterAttribute中的OnActionExecuted方法,表示在执行Action之前执行此方法
        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            //判断Action方法的Control是否跳过登录验证
            if (filterContext.ActionDescriptor.ControllerDescriptor.IsDefined(typeof(SkipCheckLoginAttribute), false))
            {
                return;
            }
            //判断Action方法是否跳过登录验证
            if (filterContext.ActionDescriptor.IsDefined(typeof(SkipCheckLoginAttribute), false))
            {
                return;
            }
            if (filterContext.HttpContext.Session["User"] == null)
            {
                //跳转方法1:
                filterContext.HttpContext.Response.Redirect("/Login/Login");
                //跳转方法2:
                ViewResult view = new ViewResult();
                //指定要返回的完整视图名称
                view.ViewName = "~/View/Login/Login.cshtml";
            }
        }
    }
}

4.一个跳过登录验证的自定义属性类SkipCheckLoginAttribute

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

namespace WebApplication4.Filters
{
    //要继承所有属性的基类Attribute
    public class SkipCheckLoginAttribute:Attribute
    {

    }
}

5.把SkipCheckLoginAttribute添加到全局过滤器中

using System.Web;
using System.Web.Mvc;
using WebApplication4.Filters;

namespace WebApplication4
{
    public class FilterConfig
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
            //添加自定义过滤器
            filters.Add(new CheckLoginAttribute());
        }
    }
}
  • 3
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值