public override void OnAuthorization(HttpActionContext filterContext)
        {
            var qs = HttpUtility.ParseQueryString(filterContext.Request.RequestUri.Query);
            string token = qs[TokenName];
            bool isValidate = false;
            //判断用户token是否有效

            if (!string.IsNullOrEmpty(token))
            {
                CheckResult result = AuthAPI4Fun.ValidateToken(token);
                if (result.code == 100)
                {
                    var entity = PersonService.Get_ByComId(result.tokenInfo.UID);
                    if (entity == null)
                    {
                        entity = PersonService.Add_Person(result.tokenInfo.Name, result.tokenInfo.UID);
                    }
                    if (entity != null)
                    {
                        isValidate = true;
                        HttpContext.Current.Session.Add(LogonUserName, new Domain.LoginUser(entity));
                    }
                }
            }


            if (!isValidate)
            {
                base.HandleUnauthorizedRequest(filterContext);
            }
        }
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var controller = filterContext.Controller as BaseController;


            var    actionName       = filterContext.RouteData.Values["Action"].ToString();
            var    controllerName   = filterContext.RouteData.Values["Controller"].ToString();
            var    actionMethodList = filterContext.Controller.GetType().GetMethods();
            string requestUrl       = filterContext.HttpContext.Request.Url.ToString();
            string token            = filterContext.HttpContext.Request["token"];
            string info             = filterContext.HttpContext.Request["info"];

            //判断用户token是否有效
            if (!string.IsNullOrEmpty(token))
            {
                CheckResult result = AuthAPI4Fun.ValidateToken(token);
                if (result.code == 100)
                {
                    var entity = PersonService.Get_ByComId(result.tokenInfo.UID);
                    if (entity == null)
                    {
                        entity = PersonService.Add_Person(result.tokenInfo.Name, result.tokenInfo.UID);
                    }
                    if (entity != null)
                    {
                        filterContext.HttpContext.Session["LoginUser"] = new Domain.LoginUser(entity);
                    }
                }
            }

            //判断页面是否需要登录
            if (allowAction.FirstOrDefault(x => x.Item1.Equals(controllerName, StringComparison.OrdinalIgnoreCase) && x.Item2.Equals(actionName, StringComparison.OrdinalIgnoreCase)) == null)
            {
                if (controller.LoginUser == null)
                {
                    if (!controllerName.Equals("login", StringComparison.OrdinalIgnoreCase))
                    {
                        var actionMethod = actionMethodList.FirstOrDefault(x => x.Name.Equals(actionName, StringComparison.OrdinalIgnoreCase));
                        if (actionMethod != null)
                        {
                            if (actionMethod.ReturnType.Name == "ViewResult" || actionMethod.ReturnType.Name == "ActionResult")
                            {
                                RedirectResult redirectResult = new RedirectResult("/login/index?redirecturl=" + requestUrl);
                                filterContext.Result = redirectResult;
                            }
                            else if (actionMethod.ReturnType.Name == "JsonResult")
                            {
                                JsonResult jsonResult = new JsonResult();
                                jsonResult.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
                                filterContext.RequestContext.HttpContext.Response.StatusCode = 9999;
                                filterContext.Result = jsonResult;
                            }
                        }
                    }
                }
            }
        }
Exemple #3
0
        /// <summary>
        /// 登录提交
        /// </summary>
        /// <param name="account">账号</param>
        /// <param name="password">密码</param>
        /// <returns></returns>
        public JsonResult Submit(string account, string password)
        {
            var person = IPersonService.Login(account, password);

            if (person != null)
            {
                this.LoginUser = new Domain.LoginUser(person);
                return(JResult(true));
            }
            else
            {
                var result = AuthAPI4Fun.Login(account, password);
                if (result != null && result.code == 100)
                {
                    person         = IPersonService.Manager_Person(result.data, account, password);
                    this.LoginUser = new Domain.LoginUser(person);
                    return(JResult(true));
                }
                else
                {
                    return(JResult(false));
                }
            }
        }