public virtual void OnRequestIncome(RequestHeader header)
 {
 }
Esempio n. 2
0
        /// <summary>
        /// Processes Alexa request AND validates request signature
        /// </summary>
        /// <param name="httpRequest"></param>
        /// <returns></returns>
        public virtual async Task <HttpResponseMessage> GetResponseAsync(HttpRequestMessage httpRequest)
        {
            DateTime now = DateTime.UtcNow; // reference time for this request


            RequestHeader requestHeader = new RequestHeader();

            requestHeader.Read(httpRequest);
            await OnRequestIncomeAsync(requestHeader);

#if HasBouncyCastle
            var cerValidator = new BouncyCastleCertValidator();
#else
            var cerValidator = new DotNetCertValidator();
#endif
            var validationResult = await cerValidator.VerifyAsync(requestHeader);

            if (!await OnRequestValidationAsync(requestHeader, validationResult))
            {
                return(new HttpResponseMessage(HttpStatusCode.BadRequest)
                {
                    ReasonPhrase = validationResult.ToString()
                });
            }

            try
            {
                RequestEnvelope = SpeechletRequestEnvelope.FromJson(requestHeader.RequestAsString);

                if (!SpeechletRequestTimestampVerifier.VerifyRequestTimestamp(RequestEnvelope, now))
                {
                    validationResult = validationResult | SpeechletRequestValidationResult.InvalidTimestamp;
                }
            }
            catch (Exception e1)
            {
                await OnParsingErrorAsync(e1);

                validationResult = SpeechletRequestValidationResult.InvalidJson;
            }


            try
            {
                string alexaResponsejson = await DoProcessRequestAsync(RequestEnvelope);

                if (alexaResponsejson == null)
                {
                    await OnResponseOutgoingAsync("->alexaResponsejson == null");

                    return(null);
                }

                await OnResponseOutgoingAsync(alexaResponsejson);

                return(new HttpResponseMessage(HttpStatusCode.OK)
                {
                    Content = new StringContent(alexaResponsejson, Encoding.UTF8, "application/json")
                });;
            }
            catch (NotImplementedException)
            {
                return(null);
            }
            catch (Exception e2)
            {
                await OnParsingErrorAsync(e2);

                return(new HttpResponseMessage(HttpStatusCode.InternalServerError));
            }
        }
 /// <summary>
 /// Opportunity to set policy for handling requests with invalid signatures and/or timestamps
 /// </summary>
 /// <returns>true if request processing should continue, otherwise false</returns>
 public virtual bool OnRequestValidation(RequestHeader header, SpeechletRequestValidationResult result)
 {
     return(result == SpeechletRequestValidationResult.OK);
 }