示例#1
0
        public async Task <HttpResponseMessage> Post()
        {
            AuthenticationHeaderValue authHeader = Request.Headers.Authorization;
            var jwtToken = authHeader?.Parameter;

            if (jwtToken != null)
            {
                try
                {
                    TokenValidation.Tokens.ValidateToken(jwtToken, secret);

                    var requestText = await Request.Content.ReadAsStringAsync();

                    var message = JsonConvert.DeserializeObject <Message>(requestText);

                    // Process the input
                    var result = Adder.Process(message.Text);

                    // Send the result back to the sender

                    var httpClient = new HttpClient();
                    httpClient.Timeout = TimeSpan.FromMinutes(60);

                    httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", TokenValidation.Tokens.MakeToken(secret, botId));
                    var reply   = new { Sender = botId, Recipient = message.Sender, Text = result };
                    var content = new StringContent(JsonConvert.SerializeObject(reply));
                    HttpResponseMessage response = await httpClient.PostAsync(message.ReplyTo, content);

                    return(response);
                }
                catch
                {
                    return(new HttpResponseMessage(HttpStatusCode.Unauthorized));
                }
            }
            return(new HttpResponseMessage(HttpStatusCode.Unauthorized));
        }