public async Task <HttpResponseMessage> ProcessMessageRequestAsync(HttpRequestMessage request, AlexaAdapter alexaAdapter, Func <ITurnContext, Task> botCallbackHandler, CancellationToken cancellationToken) { AlexaRequestBody skillRequest; byte[] requestByteArray; try { requestByteArray = await request.Content.ReadAsByteArrayAsync(); skillRequest = await request.Content.ReadAsAsync <AlexaRequestBody>(AlexaMessageMediaTypeFormatters, cancellationToken); } catch (Exception) { throw new JsonSerializationException("Invalid JSON received"); } if (skillRequest.Version != "1.0") { throw new Exception($"Unexpected version of '{skillRequest.Version}' received."); } if (_alexaOptions.ValidateIncomingAlexaRequests) { request.Headers.TryGetValues("SignatureCertChainUrl", out var certUrls); request.Headers.TryGetValues("Signature", out var signatures); var certChainUrl = certUrls.FirstOrDefault(); var signature = signatures.FirstOrDefault(); await AlexaValidateRequestSecurityHelper.Validate(skillRequest, requestByteArray, certChainUrl, signature); } var alexaResponseBody = await alexaAdapter.ProcessActivity( skillRequest, _alexaOptions, null); if (alexaResponseBody == null) { return(null); } var alexaResponseBodyJson = JsonConvert.SerializeObject(alexaResponseBody, Formatting.None, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore, ContractResolver = new DefaultContractResolver { NamingStrategy = new CamelCaseNamingStrategy() } }); var response = request.CreateResponse(HttpStatusCode.OK); response.Content = new StringContent(alexaResponseBodyJson); response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); return(response); }
protected async Task <AlexaResponseBody> ProcessMessageRequestAsync(HttpRequest request, AlexaAdapter alexaAdapter, BotCallbackHandler botCallbackHandler) { AlexaRequestBody skillRequest; var memoryStream = new MemoryStream(); request.Body.CopyTo(memoryStream); var requestBytes = memoryStream.ToArray(); memoryStream.Position = 0; using (var bodyReader = new JsonTextReader(new StreamReader(memoryStream, Encoding.UTF8))) { skillRequest = AlexaBotMessageSerializer.Deserialize <AlexaRequestBody>(bodyReader); } if (skillRequest.Version != "1.0") { throw new Exception($"Unexpected version of '{skillRequest.Version}' received."); } if (_alexaOptions.ValidateIncomingAlexaRequests) { request.Headers.TryGetValue("SignatureCertChainUrl", out var certUrls); request.Headers.TryGetValue("Signature", out var signatures); var certChainUrl = certUrls.FirstOrDefault(); var signature = signatures.FirstOrDefault(); await AlexaValidateRequestSecurityHelper.Validate(skillRequest, requestBytes, certChainUrl, signature); } var alexaResponseBody = await alexaAdapter.ProcessActivity( skillRequest, _alexaOptions, botCallbackHandler); return(alexaResponseBody); }