static async Task <bool> CreateEventAsync(NoahEvent noahEvent, string sharedSecret) { var jsonAsString = JsonConvert.SerializeObject(noahEvent); var httpContent = new StringContent(jsonAsString, Encoding.UTF8, "application/json"); var bodyForSignature = httpContent.ReadAsStringAsync().Result; // Create a Signatur of the payload string hubSignature = CreateSignature(bodyForSignature, sharedSecret); // "xxx"; client.DefaultRequestHeaders.Add("X-Hub-Signature", hubSignature); client.DefaultRequestHeaders.Add("X-Hub-Origin", "himsacloud.com"); client.DefaultRequestHeaders.Add("X-Hub-TransmissionAtempt", "1"); HttpResponseMessage response = await client.PostAsync("api/noahevents", httpContent); if (response.IsSuccessStatusCode) { return(true); } else { return(false); } }
static async Task RunAsync(string sharedSecret) { // Update port # in the following line. client.BaseAddress = new Uri("https://localhost:5001"); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add( new MediaTypeWithQualityHeaderValue("application/json")); try { Console.WriteLine("Sending a test event"); NoahEvent noahEvent = new NoahEvent() { TenantId = Guid.NewGuid(), NotificationEventId = Guid.NewGuid(), NotificationEventSubscriptionId = Guid.NewGuid() }; var result = await CreateEventAsync(noahEvent, sharedSecret); Console.WriteLine("NotificationEventId: {0}, Result: {1}", noahEvent.NotificationEventId, result); } catch (Exception e) { Console.WriteLine("Error: {0}", e.Message); } Console.ReadLine(); }
public void Enqueue(NoahEvent noahEvent) { var messageBody = JsonConvert.SerializeObject(noahEvent); CloudQueueMessage message = new CloudQueueMessage(messageBody); _queue.AddMessage(message); //throw new NotImplementedException(); }
public NoahEvent Dequeue() { CloudQueueMessage retrievedMessage = _queue.GetMessage(); if (retrievedMessage != null) { NoahEvent noahEvent = JsonConvert.DeserializeObject <NoahEvent>(retrievedMessage.AsString); _queue.DeleteMessage(retrievedMessage); return(noahEvent); } return(null); }
public IActionResult Post([FromBody] NoahEvent noahEvent) { if (ModelState.IsValid) { try { string body = ""; Request.EnableRewind(); using (var reader = new StreamReader(Request.Body)) { Request.Body.Seek(0, SeekOrigin.Begin); body = reader.ReadToEnd(); } string signatureFromHeader = Request.Headers["X-Hub-Signature"].FirstOrDefault(); string originFromHeader = Request.Headers["X-Hub-Origin"].FirstOrDefault(); string transmissionAtemptFromHeader = Request.Headers["X-Hub-TransmissionAtempt"].FirstOrDefault(); string sharedSecret = _settings.WebHookSharedSecret; // "keepmesafe"; bool isSignatureValid = SignatureValidationUtil.ValidateSignature(body, signatureFromHeader, sharedSecret); if (isSignatureValid == false) { _logger.LogError($"Noah Event Recieved was rejected - invalid signature, TenantId: {noahEvent.TenantId}, NotificationEventId: {noahEvent.NotificationEventId}, SignatureFromHeader: {signatureFromHeader}"); return(Unauthorized()); } _noahEventQueue.Enqueue(noahEvent); _logger.LogInformation($"Noah Event Recieved OK, TenantId: {noahEvent.TenantId}, NotificationEventId: {noahEvent.NotificationEventId}"); return(Ok()); } catch (Exception ex) { _logger.LogError(ex, $"Noah Event Recieved Unable to process incoming event, TenantId: {noahEvent.TenantId}, NotificationEventId: {noahEvent.NotificationEventId}"); return(BadRequest("Unable to process incoming event")); } } else { _logger.LogError($"Noah Event Recieved Invalid Message, TenantId: {noahEvent.TenantId}, NotificationEventId: {noahEvent.NotificationEventId}"); return(BadRequest(ModelState)); } }
public void Enqueue(NoahEvent noahEvent) { _eventQueue.Enqueue(noahEvent); }
public void ProcessEvent(NoahEvent noahEvent) { var jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(noahEvent); Console.Write(jsonString); }