Exemplo n.º 1
0
        /// <summary>
        /// 统一验证Session[Keys.uinfo]如果为null则跳转到登陆页
        /// </summary>
        /// <param name="filterContext"></param>
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            //0.0判断是否有贴跳过登录检查的特性标签
            if (filterContext.ActionDescriptor.ControllerDescriptor.IsDefined(typeof(SkipCheckLogin), false))
            {
                return;
            }

            if (filterContext.ActionDescriptor.IsDefined(typeof(SkipCheckLogin), false))
            {
                return;
            }

            //1.0 判断session是否为null
            if (filterContext.HttpContext.Session[Keys.uinfo] == null)
            {
                //1.0.1 查询cookie[Keys.Ismemeber]是否不为null,如果成立则模拟用户的登录,再将用户实体数据存入session[Keys.uinfo]中
                if (filterContext.HttpContext.Request.Cookies[Keys.IsMember] != null)
                {
                    //1.0 取出cookie中存入的uid的值
                    string uid = filterContext.HttpContext.Request.Cookies[Keys.IsMember].Value;
                    uid = DESEncrypt.Decrypt(uid);

                    //2.0 根据uid查询出用户的实体

                    //2.0.1 从缓存中获取autofac的容器对象
                    var cont = CacheMgr.GetData <IContainer>(Keys.AutofacContainer);
                    //2.0.2 找autofac容器获取IsysUserInfoServices接口的具体实现类的对象实例
                    IsysUserInfoServices userSer = cont.Resolve <IsysUserInfoServices>();

                    //2.0.3 根据userSer 集合uid查询数据
                    int iuserid  = int.Parse(uid);
                    var userinfo = userSer.QueryWhere(c => c.uID == iuserid).FirstOrDefault();
                    if (userinfo != null)
                    {
                        //2.0.4 将userinfo存入session
                        filterContext.HttpContext.Session[Keys.uinfo] = userinfo;
                    }
                    else
                    {
                        ToLogin(filterContext);
                    }
                }
                else
                {
                    //2.0 跳转到登录页面
                    // filterContext.HttpContext.Response.Redirect("/admin/login/login");

                    //ContentResult cr = new ContentResult();
                    //cr.Content = "<script>alert('您未登录');window.location='/admin/login/login';</script>";

                    ToLogin(filterContext);
                }
            }

            base.OnActionExecuting(filterContext);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 负责验证Session[Keys.Uinfo]是否为null,如果为null则直接跳转到登录页面
        /// </summary>
        /// <param name="filterContext"></param>
        public override void OnActionExecuting(HttpActionContext filterContext)
        {
            //0.0 判断控制器类或者action是否有贴SkipCheckLogin标签,如果有贴则阻断下面代码的运行
            if (filterContext.ControllerContext.ControllerDescriptor.GetCustomAttributes <SkipCheckLoginAttribute>().Count == 1)
            {
                return;
            }

            if (filterContext.ActionDescriptor.GetCustomAttributes <SkipCheckLoginAttribute>().Count == 1)
            {
                return;
            }

            //1.0 判断session如果为空则跳转
            if (HttpContext.Current.Session[Keys.Uinfo] == null)
            {
                //1.0 第一种提醒方式,体验不好,因为会导致页面泛白
                //filterContext.HttpContext.Response.Write("<script>alert('您未登录');window.location='/admin/login/login'</script>");

                //2.0 判断如果cookie中的数据不为空,则应该取出其中的用户主键去sysuserinfo表中再次获取实体
                //存入session
                if (HttpContext.Current.Request.Cookies[Keys.IsRemember] != null)
                {
                    //2.0.1 获取用户主键
                    string userid = HttpContext.Current.Request.Cookies[Keys.IsRemember].Value;

                    //2.0.2 根据userid去访问sysuserinfo获取数据实体
                    //2.0.2.1 从全局缓存中获取autofac的容器对象
                    IContainer autofac = CacheMgr.GetData <IContainer>(Keys.autofaccontainer);
                    //2.0.2.2 从autofac容器中获取IsysUserInfoServices的实现类的对象实例
                    IsysUserInfoServices userSer = autofac.Resolve <IsysUserInfoServices>();

                    int uid = int.Parse(userid);

                    var userinfo = userSer.QueryWhere(c => c.uID == uid).FirstOrDefault();

                    //3.0 判断userinfo是否为null
                    if (userinfo == null)
                    {
                        ToLogin(filterContext);
                    }
                    else
                    {
                        //4.0 将userinfo实体对象存入session
                        filterContext.HttpContext.Session[Keys.Uinfo] = userinfo;
                    }
                }
                else
                {
                    //统一跳转
                    ToLogin(filterContext);
                }
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// 统一验证session【keys.uinfo】如果是null跳转到登录页
        /// </summary>
        /// <param name="filterContext"></param>
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            //判断是否有贴跳过登录检查的特性标签
            if (filterContext.ActionDescriptor.ControllerDescriptor.IsDefined(typeof(SkipCheckLogin), false))
            {
                return;
            }
            if (filterContext.ActionDescriptor.IsDefined(typeof(SkipCheckLogin), false))
            {
                return;
            }
            if (filterContext.HttpContext.Session[Keys.uinfo] == null)
            {
                //1.0查询cookie【keys.Ismember]是否为空,如果成立则模拟用户的登录,
                //在将用户实体数据存入session[keys.uinfo】中

                if (filterContext.HttpContext.Request.Cookies[Keys.IsMember] != null)
                {
                    //1.0取出cookie中存入的uid的值
                    string uid = filterContext.HttpContext.Request.Cookies[Keys.IsMember].Value;

                    //2.0根据uid查询出实体
                    //2.00先从缓存中获取autofac的容器对象
                    var cont = CacheMgr.GetData <IContainer>(Keys.AutofacContainer);
                    //2.01找autofac容器获取IsysUserInfoServices
                    IsysUserInfoServices userSer = cont.Resolve <IsysUserInfoServices>();
                    //2.03根据userser集合uid查询数据
                    int iuserid  = int.Parse(uid);
                    var userinfo = userSer.QueryWhere(c => c.uID == iuserid).FirstOrDefault();
                    if (userinfo != null)
                    {
                        filterContext.HttpContext.Session[Keys.uinfo] = userinfo;
                    }
                    else
                    {
                        ToLogin(filterContext);
                    }
                }

                ////法一:
                // filterContext.HttpContext.Response.Redirect("/Admin/Login/Login");
                //法二:
                //ContentResult cr = new ContentResult();
                //cr.Content = "<script>alert('您未登录');window.location='/Admin/Login/Login'</script>";
                //filterContext.Result = cr;
                else
                {
                    ToLogin(filterContext);
                }
            }
            base.OnActionExecuting(filterContext);
        }
Exemplo n.º 4
0
        public static sysUserInfo GetUserByID(object userid)
        {
            if (userid == null)
            {
                return(new sysUserInfo()
                {
                });
            }
            int iuser = int.Parse(userid.ToString());

            var autofac = CacheMgr.GetData <IContainer>(Keys.AutofacContainer);
            IsysUserInfoServices userSer = autofac.Resolve <IsysUserInfoServices>();

            return(userSer.QueryWhere(c => c.uID == iuser).FirstOrDefault());
        }
Exemplo n.º 5
0
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            //1.0 判断如果贴有    [SkipCheckLogin]则跳过登录检查
            if (filterContext.ActionDescriptor.ControllerDescriptor.IsDefined(typeof(SkipCheckLoginAttribute), false))
            {
                return;
            }

            if (filterContext.ActionDescriptor.IsDefined(typeof(SkipCheckLoginAttribute), false))
            {
                return;
            }

            if (filterContext.HttpContext.Session[Keys.uinfo] == null)
            {
                //1.0 判断cookie是否有值
                if (filterContext.HttpContext.Request.Cookies[Keys.isremember] == null)
                {
                    //2.0 跳转到登录页面
                    ToLogin(filterContext);
                }
                else
                {
                    //3.0 获取cookie中存好的用户id
                    string uid = filterContext.HttpContext.Request.Cookies[Keys.isremember].Value;

                    //4.0 存全局缓存中获取autofac容器对象
                    var container = CacheMgr.GetData <IContainer>(Keys.autofac);

                    //4.0 根据uid去表sysuserinfo中查询用户对象重新赋值给Session[Keys.uinfo]
                    IsysUserInfoServices userServics = container.Resolve <IsysUserInfoServices>();
                    int iuserid  = uid.AsInt();
                    var userinfo = userServics.QueryWhere(c => c.uID == iuserid).FirstOrDefault();
                    if (userinfo == null)
                    {
                        ToLogin(filterContext);
                    }
                    else
                    {
                        filterContext.HttpContext.Session[Keys.uinfo] = userinfo;

                        //设置缓存
                        //container.Resolve<IsysPermissListServices>().GetPermissListByUid(userinfo.uID);
                    }
                }
            }
        }
Exemplo n.º 6
0
        public ActionResult Login(LoginInfoViewModels model)
        {
            string vcodeFromSession = string.Empty;

            if (Session[Keys.vcode] != null)
            {
                vcodeFromSession = Session[Keys.vcode].ToString();
            }
            if (model.VCode.IsEmpty() || vcodeFromSession.Equals(model.VCode, StringComparison.OrdinalIgnoreCase) == false)
            {
                return(WriteError("验证码不合法"));
            }
            var userinfo = UserInfoServices.QueryWhere(c => c.uLoginName == model.uLoginName && c.uLoginPWD == model.uLoginPwd).FirstOrDefault();

            if (userinfo == null)
            {
                return(WriteError("用户名或者密码错误"));
            }
            // Session[Keys.uinfo] = userinfo;
            //改用redis缓存
            string sessionId = Guid.NewGuid().ToString("N");//必须保证Memcache的key唯一

            cacheManager.Set(sessionId, userinfo, TimeSpan.FromHours(1));
            Response.Cookies[Keys.uinfo].Value = sessionId;//将自创的用户信息以Cookie的形式返回给浏览器。

            if (model.IsMember)
            {
                HttpCookie cookie = new HttpCookie(Keys.IsMember, userinfo.uID.ToString());
                cookie.Expires = DateTime.Now.AddDays(3);
                Response.Cookies.Add(cookie);
            }
            else
            {
                HttpCookie cookie = new HttpCookie(Keys.IsMember, "");
                cookie.Expires = DateTime.Now.AddYears(-3);
                Response.Cookies.Add(cookie);
            }

            return(WriteSuccess("登录成功"));
        }
Exemplo n.º 7
0
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            HttpContext context = HttpContext.Current;
            //从缓存中获取autofac的容器对象
            var cont = CacheMgr.GetData <IContainer>(Keys.AutofacContainer);
            //获取到依赖注入数据
            ICacheManager cacheManager = cont.Resolve <ICacheManager>();

            if (filterContext.ActionDescriptor.ControllerDescriptor.IsDefined(typeof(SkipCheckLogin), false))
            {
                return;
            }
            if (filterContext.ActionDescriptor.IsDefined(typeof(SkipCheckLogin), false))
            {
                return;
            }
            //判断session是否为null
            if (filterContext.HttpContext.Request.Cookies[Keys.uinfo] == null)
            {
                if (filterContext.HttpContext.Request.Cookies[Keys.IsMember] != null)
                {
                    //取出cookie中存入的uid的值
                    string uid = filterContext.HttpContext.Request.Cookies[Keys.IsMember].Value;

                    //获取到依赖注入数据
                    IsysUserInfoServices userSer = cont.Resolve <IsysUserInfoServices>();

                    int iuserid  = int.Parse(uid);
                    var userinfo = userSer.QueryWhere(c => c.uID == iuserid).FirstOrDefault();
                    if (userinfo != null)
                    {
                        //将userinfo存入session
                        //filterContext.HttpContext.Session[Keys.uinfo] = userinfo;

                        //改用redis存储用户信息

                        //改用redis缓存
                        string sessionId = Guid.NewGuid().ToString("N");//必须保证Memcache的key唯一
                        cacheManager.Set(sessionId, userinfo, TimeSpan.FromHours(1));
                        //filterContext.HttpContext.Request.Cookies[Keys.uinfo].Value = sessionId;
                        context.Response.Cookies[Keys.uinfo].Value = sessionId;//将自创的用户信息以Cookie的形式返回给浏览器。
                        BaseController.LoginUser = userinfo;
                    }
                    else
                    {
                        ToLogin(filterContext);
                    }
                }
                else
                {
                    ToLogin(filterContext);
                }
            }
            else
            {
                string      sessionId = filterContext.HttpContext.Request.Cookies[Keys.uinfo].Value;
                sysUserInfo obj       = cacheManager.Get <sysUserInfo>(sessionId);//获取Memcache中的数据.
                if (obj != null)
                {
                    BaseController.LoginUser = obj;
                    //模拟滑动过期时间。
                    cacheManager.Set(sessionId, obj, TimeSpan.FromHours(1));
                }
                else
                {
                    ToLogin(filterContext);
                }
            }
        }