コード例 #1
0
        public Task OnAuthorizationAsync(AuthorizationFilterContext context)
        {
            _logger.LogInformation("Authorize request header");
            var correlationHeader = context.HttpContext.Request.Headers[Identifiers.CorrelationId];
            //TODO:replace the following correlation id, since it's correlating all operation from this assembly instance.
            var correlationId = _operationalUnit.InstanceId;

            if (!string.IsNullOrEmpty(correlationHeader) && Guid.TryParse(correlationHeader, out Guid paresedCorId))
            {
                correlationId = paresedCorId;
            }

            //TODO: remove bypassing authorization
            if (false && string.IsNullOrEmpty(context.HttpContext.Request.Headers["authorization"]))
            {
                var messageHeader = new MessageHeader {
                    CorrelationId = correlationId
                };

                var messageFooter = new MessageFooter
                {
                    Sender      = context.ActionDescriptor.DisplayName,
                    Environment = _operationalUnit.Environment,
                    Assembly    = _operationalUnit.Assembly,
                    FingerPrint = context.ActionDescriptor.Id,
                    Route       = JsonConvert.SerializeObject(context.RouteData.Values.ToDictionary(key => key.Key, value => value.Value?.ToString()), Defaults.JsonSerializerSettings),
                    Hint        = Enum.GetName(typeof(ResponseHint), ResponseHint.UnAuthorized)
                };
                messageHeader.GetType().GetProperties()
                .ToList().ForEach(prop =>
                {
                    context.HttpContext.Response.Headers.Add(prop.Name, new StringValues(prop.GetValue(messageHeader)?.ToString()));
                });
                messageFooter.GetType().GetProperties()
                .ToList().ForEach(prop =>
                {
                    context.HttpContext.Response.Headers.Add(prop.Name, new StringValues(prop.GetValue(messageFooter)?.ToString()));
                });

                context.Result = new ContentResult()
                {
                    StatusCode  = context.HttpContext.Response.StatusCode,
                    ContentType = Identifiers.ApplicationJson,
                    Content     = JsonConvert.SerializeObject(Enum.GetName(typeof(ResponseHint), ResponseHint.UnAuthorized), Defaults.JsonSerializerSettings)
                };
                _logger.LogInformation("Override response!");
                return(Task.CompletedTask);
            }
            return(Task.CompletedTask);
        }
コード例 #2
0
        public Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next)
        {
            var correlationHeader = context.HttpContext.Request.Headers[Identifiers.CorrelationId];
            //TODO:replace the following correlation id, since it's correlating all operation from this assembly instance.
            var correlationId = _operationalUnit.InstanceId;

            if (!string.IsNullOrEmpty(correlationHeader) && Guid.TryParse(correlationHeader, out Guid paresedCorId))
            {
                correlationId = paresedCorId;
            }
            var messageHeader = new MessageHeader(isSucceed: context.HttpContext.Response.StatusCode == 200)
            {
                CorrelationId = correlationId
            };

            var messageFooter = new MessageFooter
            {
                Sender      = context.ActionDescriptor.DisplayName,
                Environment = _operationalUnit.Environment,
                Assembly    = _operationalUnit.Assembly,
                FingerPrint = context.ActionDescriptor.Id,
                Route       = JsonConvert.SerializeObject(context.RouteData.Values.ToDictionary(key => key.Key, value => value.Value?.ToString()), Defaults.JsonSerializerSettings),
                Hint        = Enum.GetName(typeof(ResponseHint), ResponseHint.OK)
                              //TODO: infer the hint from HTTP status code
            };

            messageHeader.GetType().GetProperties()
            .ToList().ForEach(prop =>
            {
                context.HttpContext.Response.Headers.Add(prop.Name, new StringValues(prop.GetValue(messageHeader)?.ToString()));
            });
            messageFooter.GetType().GetProperties()
            .ToList().ForEach(prop =>
            {
                context.HttpContext.Response.Headers.Add(prop.Name, new StringValues(prop.GetValue(messageFooter)?.ToString()));
            });

            context.Result = new ContentResult()
            {
                StatusCode  = context.HttpContext.Response.StatusCode,
                ContentType = Identifiers.ApplicationJson,
                Content     = JsonConvert.SerializeObject(getContentResult(context.Result), Defaults.JsonSerializerSettings)
            };
            _logger.LogInformation("Override response!");
            return(next.Invoke());

            object getContentResult(IActionResult result)
            {
                switch (result)
                {
                case ContentResult CntR:
                {
                    return(CntR?.Content);
                }

                case ObjectResult ObjR:
                {
                    return(ObjR?.Value);
                }

                case JsonResult JsonR:
                {
                    return(JsonR?.Value);
                }

                default:
                {
                    return(result);
                }
                }
            };
        }
コード例 #3
0
ファイル: ConstantContactAPI.cs プロジェクト: WCGPR0/parsers
        private IEnumerable <EmailCampaign> CreateCampaigns(Dictionary <string, string> parameters)
        {
            XDocument emailTemplate = new XDocument();

            using (System.Xml.XmlWriter writer = emailTemplate.CreateWriter())
            {
                XslCompiledTransform emailTemplateTransform = new XslCompiledTransform();
                XsltArgumentList     emailTemplateArgList   = new XsltArgumentList();
                foreach (KeyValuePair <string, string> parameter in parameters)
                {
                    emailTemplateArgList.AddParam(parameter.Key, "", parameter.Value);
                }
                emailTemplateTransform.Load(_emailTemplateTransformUrl);
                emailTemplateTransform.Transform(createEmailConnectionXML().CreateReader(), emailTemplateArgList, writer);
            }
            List <EmailCampaign> campaigns = new List <EmailCampaign>();

            foreach (XElement campaignElement in emailTemplate.Descendants("campaign"))
            {
                EmailCampaign campaign = new EmailCampaign();
                campaign.EmailContentFormat = CampaignEmailFormat.XHTML;
                campaign.Status             = CampaignStatus.SENT;
                foreach (XElement property in campaignElement.Elements("property"))
                {
                    string name  = property.Attribute("name").Value;
                    string value = String.Concat(property.Nodes());
                    try
                    {
                        campaign.GetType().GetProperty(name).SetValue(campaign, value, null);
                    }
                    catch (Exception)
                    {
                        throw new Exception($"Error with campaign property setting, {name}, with value, {value}");
                    }
                }
                MessageFooter msgFooter = new MessageFooter();
                campaign.MessageFooter = msgFooter;
                foreach (XElement method in emailTemplate.Descendants("footer").Elements("property"))
                {
                    string name  = method.Attribute("name").Value;
                    string value = method.Value;
                    try
                    {
                        msgFooter.GetType().GetProperty(name).SetValue(msgFooter, value, null);
                    }
                    catch (Exception)
                    {
                        throw new Exception($"Error with footer property setting, {name}, with value, {value}");
                    }
                }
                List <SentContactList> lists = new List <SentContactList>();
                foreach (XElement method in emailTemplate.Descendants("contacts").Elements("contact"))
                {
                    string          id          = method.Attribute("id").Value;
                    SentContactList contactList = new SentContactList()
                    {
                        Id = id
                    };
                    lists.Add(contactList);
                }
                campaign.Lists = lists;
                campaigns.Add(campaign);
            }
            return(campaigns);
        }
コード例 #4
0
        public Task OnExceptionAsync(ExceptionContext context)
        {
            _logger.LogCritical(context.Exception, context.Exception.Message);

            var correlationHeader = context.HttpContext.Request.Headers[Identifiers.CorrelationId];
            //TODO:replace the following correlation id, since it's correlating all operation from this assembly instance.
            var correlationId = _operationalUnit.InstanceId;

            if (!string.IsNullOrEmpty(correlationHeader) && Guid.TryParse(correlationHeader, out Guid paresedCorId))
            {
                correlationId = paresedCorId;
            }

            var exception = context.Exception;

            (int code, string message, ResponseHint responseHint) = (exception is CustomException ex) ? ex.CustomMessage : (exception.HResult, exception.Message, ResponseHint.SystemError);

            var messageHeader = new MessageHeader(isSucceed: false)
            {
                CorrelationId = correlationId
            };

            var messageFooter = new MessageFooter
            {
                Sender      = context.ActionDescriptor.DisplayName,
                Environment = _operationalUnit.Environment,
                Assembly    = _operationalUnit.Assembly,
                FingerPrint = context.ActionDescriptor.Id,
                Route       = JsonConvert.SerializeObject(context.RouteData.Values.ToDictionary(key => key.Key, value => value.Value?.ToString()), Defaults.JsonSerializerSettings),
                Hint        = Enum.GetName(typeof(ResponseHint), responseHint)
            };

            context.ExceptionHandled = true;
            messageHeader.GetType().GetProperties()
            .ToList().ForEach(prop =>
            {
                context.HttpContext.Response.Headers.Add(prop.Name, new StringValues(prop.GetValue(messageHeader)?.ToString()));
            });
            messageFooter.GetType().GetProperties()
            .ToList().ForEach(prop =>
            {
                context.HttpContext.Response.Headers.Add(prop.Name, new StringValues(prop.GetValue(messageFooter)?.ToString()));
            });

            context.Result = new ContentResult()
            {
                StatusCode  = StatusCodes.Status500InternalServerError,
                ContentType = Identifiers.ApplicationJson,
                Content     = JsonConvert.SerializeObject(closureGenerateErrorMessage(), Defaults.JsonSerializerSettings)
            };

            _logger.LogInformation("Override response!");
            return(Task.CompletedTask);

            object closureGenerateErrorMessage()
            {
                return(new
                {
                    //TODO: system message only should share user friendly messages for system messages, as well to not break system security.
                    code,
                    message,
                    systemMessage = exception.Message
                });
            }
        }