.NET MVC中登录授权过滤器的使用

.NET MVC中登录授权过滤器的使用

1、写个类LoginAuthorityAttribute,继承自AuthorizeAttribute

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

namespace PowerBIDocs.Web.Utils
{
    public class LoginAuthorityAttribute : AuthorizeAttribute
    {
        public override void OnAuthorization(AuthorizationContext filterContext)
        {
            if (App_Start.GlobalConfig.LoginedUser == null)
            {
                filterContext.HttpContext.Response.Redirect("~/Home/Login");
            }
        }
    }
}
TEXT

2、在所有需要登陆才能访问的控制器中的方法上面,标注: [LoginAuthority]

      [LoginAuthority]
        public ActionResult Logout()
        {
            HttpContext.Session[App_Start.GlobalConfig.LoginedUserSessionKey] = null;
            return RedirectToAction("Login");
        }
TEXT

3、说明:上面的例子中,用户信息存在于SESSION中。


.NET MVC中登录授权过滤器的使用
https://www.dearcloud.cn/2017/12/06/20200310-cnblogs-old-posts/20171206-.NETMVC中登陆授权过滤器的使用/
作者
宋兴柱
发布于
2017年12月6日
许可协议