public async Task Invoke(HttpContext context)
        {
            var req = context.Request;

            if (!(await _certVerifier.IsCertificateValidAsync(req)))
            {
                context.Response.StatusCode = (int)System.Net.HttpStatusCode.BadRequest;
            }

            await this._next(context);
        }
        public async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            string textContent = null;

            bool isValid = false;

            try
            {
                isValid = await _reqVerifier.IsCertificateValidAsync(req);
            }
            catch (Exception ex)
            {
                log.LogError(ex, "Error processing certificate");
            }

            if (!isValid)
            {
                return(new BadRequestResult());
            }

            AlexaRequest alexaRequest = null;

            try
            {
                using (StreamReader sr = new StreamReader(req.Body))
                {
                    //This allows you to do one Read operation.
                    textContent = sr.ReadToEnd();
                }

                alexaRequest = JsonConvert.DeserializeObject <AlexaRequest>(textContent);
            }
            catch (Exception ex)
            {
                log.LogError(ex, $"Error processing alexa request: {textContent}");
            }


            var alexaResponse = await _adventureProcessor.ProcessAdventureRequestAsync(alexaRequest);

            return((ActionResult) new OkObjectResult(alexaResponse));
        }
Example #3
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log,
            [Inject] IAlexaRequestVerifier alexaVerifier,
            [Inject] IAdventureSampleProcessor adventureProcessor)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            string textContent = null;

            //IServiceProvider servProv = _serviceProvider.Value;

            //IAlexaRequestVerifier alexaVerifier = servProv.GetRequiredService<IAlexaRequestVerifier>();

            bool isValid = false;

            try
            {
                isValid = await alexaVerifier.IsCertificateValidAsync(req);
            }
            catch (Exception ex)
            {
                log.LogError(ex, "Error processing certificate");
            }

            if (!isValid)
            {
                return(new BadRequestResult());
            }

            AlexaRequest alexaRequest = null;

            try
            {
                using (StreamReader sr = new StreamReader(req.Body))
                {
                    //This allows you to do one Read operation.
                    textContent = sr.ReadToEnd();
                }

                alexaRequest = JsonConvert.DeserializeObject <AlexaRequest>(textContent);
            }
            catch (Exception ex)
            {
                log.LogError(ex, $"Error processing alexa request: {textContent}");
            }

            if (alexaVerifier.IsRequestValid(alexaRequest))
            {
                var alexaResponse = await adventureProcessor.ProcessAdventureRequestAsync(alexaRequest);

                return((ActionResult) new OkObjectResult(alexaResponse));
            }
            else
            {
                log.LogError($"Alexa request is not valid: {textContent}");
            }


            return(new BadRequestResult());
        }