授权登录

1.创建一个类(写授权的类 Authorization)

2 在过滤器 FilterConfig.cs  添加一个写授权的类引用

using Mvc.guoluqi;  //引用授权的类

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
            filters.Add(new shouquan()); //添加授权
        }

创建的类的文件夹

3.在类中写:

 public class shouquan:IAuthorizationFilter(继承这个类)
    {
        public void OnAuthorization(AuthorizationContext filterContext)
        {
            string controllerName = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName; //获取控制器的名称
            
            if (controllerName.ToLower() == "mvc_login")  //判断控制器的名称是否是登录控制器
            {

            }
            else
            {
                if (filterContext.HttpContext.Session["deng"] == null) //判断页面的登录是否有值,没有值就跳转登录页面
                {
                    filterContext.Result = new RedirectResult("/MVC_Login/login");
                }
            }
        }
    }

 

posted on 2017-09-28 20:35  苏了个酥i  阅读(208)  评论(0编辑  收藏  举报