• Filter全局登录验证


      //检查登录
        public class CheckLoginFilter : IAuthorizationFilter
        {
            public void OnAuthorization(AuthorizationContext filterContext)
            {
                //不验证属性则只需取值
                if (filterContext.ActionDescriptor.IsDefined(typeof(AllowAnonymousAttribute), true) || filterContext.ActionDescriptor.ControllerDescriptor.IsDefined(typeof(AllowAnonymousAttribute), true))
                {
                    if (filterContext.HttpContext.Request.Cookies["token"] != null) {
                        string token = filterContext.HttpContext.Request.Cookies["token"].Value;
                        if (token != "")
                        {
                            //如果cookie存在则判断Session
                            if (filterContext.HttpContext.Session[token] == null)
                            {
                                GuserService bll = new GuserService();
                                GuserDTO model = bll.GetUserByToken(token);
                                if (model == null) {
                                    return;
                                }
                                filterContext.HttpContext.Session[token] = model;
                                filterContext.Controller.ViewBag.UserName = model.userName;
                                filterContext.Controller.ViewBag.Uid = model.id;
                            }
                            else
                            {
                                GuserDTO model = (GuserDTO)filterContext.HttpContext.Session[token];
                                filterContext.Controller.ViewBag.UserName = model.userName;
                                filterContext.Controller.ViewBag.Uid = model.id;
    
                                return;
                            }
                        }
                        else {
                            return;
                        }
                    }
                    return;
                }
                else {
                    string actionName = filterContext.ActionDescriptor.ActionName;
                    string ctrlName = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName;
                    string returnUrl = "?returnUrl=/" + ctrlName + "/" + actionName; 
                    //判断储存token的Cookie存在与否
                    if (filterContext.HttpContext.Request.Cookies["token"] == null)
                    {
                        filterContext.Result = new RedirectResult("/Home/Login"+returnUrl);
                    }
                    else {
                        string token = filterContext.HttpContext.Request.Cookies["token"].Value;
                        if (token == "")
                        {
                            filterContext.Result = new RedirectResult("/Home/Login"+returnUrl);
                        }
                        else
                        {
                            //如果cookie存在则判断Session
                            if (filterContext.HttpContext.Session[token] == null)
                            {
                                GuserService bll = new GuserService();
                                GuserDTO model = bll.GetUserByToken(token);
                                filterContext.HttpContext.Session[token] = model;
                                filterContext.Controller.ViewBag.UserName = model.userName;
                                filterContext.Controller.ViewBag.Uid = model.id;
                            }
                            else {
                                GuserDTO model = (GuserDTO)filterContext.HttpContext.Session[token];
                                filterContext.Controller.ViewBag.UserName = model.userName;
                                filterContext.Controller.ViewBag.Uid = model.id;
                            }
                        }
                    }
                }
            }
        }
    

      

  • 相关阅读:
    windows ntstatus.h 头文件
    Android Q 后台启动 Activity
    windows 删除文件夹所有文件夹及文件代码
    Android 加壳App Demo
    Android App 签名保护demo
    RXAndroidBle 记录网址
    c++ windows 获取系统时间
    js 代码保存
    day33 ansible
    day31 综合实时同步服务
  • 原文地址:https://www.cnblogs.com/Kuleft/p/11088160.html
Copyright © 2020-2023  润新知