欢迎来到我的的博客园,祝大家学有所成,早点实现自己的人生理想。

.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");
            }
        }
    }
}

 

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

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

 

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

  

posted @ 2017-12-06 10:05  宋兴柱  阅读(715)  评论(0编辑  收藏  举报