public Task Execute(IJobExecutionContext context)
        {
            var data = JsonSerializerHelper.Deserialize <Data.Models.TaskScheduling>(context.JobDetail.JobDataMap.GetString("data"));

            Log4Net.InfoLog("run job:" + data.Id);
            DateTime runTime     = context.FireTimeUtc.LocalDateTime;
            DateTime?nextRunTime = null;

            if (context.NextFireTimeUtc != null)
            {
                nextRunTime = context.NextFireTimeUtc.Value.LocalDateTime;
            }
            string result = "";

            try
            {
                result = httpRequest.Get(data.Api, null);
            }
            catch (Exception ex)
            {
                Log4Net.ErrorLog(ex);
                result = ex.Message;
            }
            try
            {
                schedulingHistory.InsertHistoryAndUpdateScheduling(data.Id, data.Name, runTime, nextRunTime, result);
            }
            catch (Exception ex)
            {
                Log4Net.ErrorLog(ex);
            }
            return(Task.Delay(0));
        }
        private string ExecuteRequest(string method, string command, string jsonData)
        {
            WebException ex = null;

            for (var i = 0; i < count; i++)
            {
                //从队列获取一个连接
                string uri = connections.Peek();
                uri = uri.TrimEnd('/') + "/" + command.TrimStart('/');
                try
                {
                    HttpWebRequest request = CreateRequest(method, uri);
                    if (!string.IsNullOrEmpty(jsonData))
                    {
                        byte[] buffer = Encoding.UTF8.GetBytes(jsonData);
                        request.ContentLength = buffer.Length;
                        using (Stream requestStream = request.GetRequestStream())
                        {
                            requestStream.Write(buffer, 0, buffer.Length);
                        }
                    }
                    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                    {
                        using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                        {
                            return(reader.ReadToEnd());
                        }
                    }
                }
                catch (WebException webException)
                {
                    if (webException.Response != null && ((HttpWebResponse)webException.Response).StatusCode == HttpStatusCode.NotFound)
                    {
                        return("404");
                    }
                    //从队列获取的连接不可用
                    string unuseConnect = connections.Dequeue();
                    //把不可用的连接放入队尾
                    connections.Enqueue(unuseConnect);
                    //通知维护人员
                    Log4Net.ErrorLog(webException);
                }
            }
            throw ex;
        }
Beispiel #3
0
        public void OnAuthorization(AuthorizationContext filterContext)
        {
            var reflectedActionDescriptor = (ReflectedActionDescriptor)filterContext.ActionDescriptor;
            IEnumerable <CustomAttributeData> methodAttributes     = reflectedActionDescriptor.MethodInfo.CustomAttributes;
            IEnumerable <CustomAttributeData> controllerAttributes = reflectedActionDescriptor.ControllerDescriptor.ControllerType.CustomAttributes;
            bool   isAuthorization = true;
            string permissionName  = "";

            foreach (CustomAttributeData item in controllerAttributes)
            {
                if (item.AttributeType.Name == "AllowAnonymousAttribute")
                {
                    isAuthorization = false;
                }
                if (item.AttributeType.Name == "JwtAuthorizeAttribute")
                {
                    isAuthorization = true;
                    if (item.ConstructorArguments.Count > 0)
                    {
                        permissionName = item.ConstructorArguments[0].Value.ToString();
                    }
                }
            }
            foreach (CustomAttributeData item in methodAttributes)
            {
                if (item.AttributeType.Name == "AllowAnonymousAttribute")
                {
                    isAuthorization = false;
                }
                if (item.AttributeType.Name == "JwtAuthorizeAttribute")
                {
                    isAuthorization = true;
                    if (item.ConstructorArguments.Count > 0)
                    {
                        permissionName = item.ConstructorArguments[0].Value.ToString();
                    }
                }
            }
            //如果设置了匿名访问直接返回
            if (!isAuthorization)
            {
                return;
            }
            HttpRequestBase request       = filterContext.HttpContext.Request;
            string          authorization = JwtManager.GetAuthorization(request, ssoCookieKey);

            if (string.IsNullOrEmpty(authorization))
            {
                filterContext.Result = new ResponseModel <string>(ErrorCode.authorize_fault, "");
            }
            else
            {
                if (!filterContext.HttpContext.Items.Contains("Authorization"))
                {
                    filterContext.HttpContext.Items.Add("Authorization", authorization);
                }
                try
                {
                    var principal = JwtManager.ParseAuthorization(authorization, ssoSecretKey);
                    filterContext.HttpContext.User = principal;
                    if (!CheckPermission(permissionName, filterContext.HttpContext.User.Identity.Name))
                    {
                        filterContext.Result = new ResponseModel <string>(ErrorCode.error_permission, "");
                    }
                }
                catch (SecurityTokenInvalidAudienceException ex) //Audience Error
                {
                    Log4Net.ErrorLog(ex);
                    filterContext.Result = new ResponseModel <string>(ErrorCode.invalid_token, "");
                }
                catch (SecurityTokenExpiredException ex) //expried token
                {
                    Log4Net.ErrorLog(ex);
                    filterContext.Result = new ResponseModel <string>(ErrorCode.token_expired, "");
                }
                catch (Exception ex)
                {
                    Log4Net.ErrorLog(ex);
                    filterContext.Result = new ResponseModel <string>(ErrorCode.invalid_token, "");
                }
            }
        }