using System.Net; using System.Security.Cryptography; namespace Test { public class Program { private const string SECRET_KEY = "arsuwuxsy2lesiq7a835gdttu39"; public static void Main(string[] args) { var builder = WebApplication.CreateBuilder(args); var app = builder.Build(); app.UseHttpsRedirection(); app.MapPost("/mywebhookurl", async (HttpContext context) => { try { string senderUrl = context.Request.Headers["X-Telligent-Webhook-Sender"].FirstOrDefault(); string hashSignature = context.Request.Headers["X-Telligent-Webhook-Signature"].FirstOrDefault(); string rawPostData; using (var reader = new System.IO.StreamReader(context.Request.Body)) { rawPostData = await reader.ReadToEndAsync(); } // Validate the posted data's authenticity string calculatedSignature = CalculateSignature(rawPostData, SECRET_KEY); if (!hashSignature.Equals(calculatedSignature)) { // The signatures do mot match only if the wrong secret is used, // or the data is not from the community site. context.Response.StatusCode = (int)HttpStatusCode.BadRequest; System.Console.WriteLine("Signature validation failed."); } var webhookEvents = System.Text.Json.JsonSerializer.Deserialize(rawPostData); foreach (var webhookEvent in webhookEvents.events) { // Ensure that the event type is the one you want to handle if (webhookEvent.TypeId == "98b84792-bbcc-4c27-8df3-f594322b5087") // Blog Post Created { var userId = Int32.Parse(webhookEvent.EventData["ActorUserId"].ToString()); var contentId = Guid.Parse(webhookEvent.EventData["ContentId"].ToString()); var blogPostId = Int32.Parse(webhookEvent.EventData["BlogPostId"].ToString()); // TODO: do something with the data System.Console.WriteLine($"Blog Post Created: userId: {userId}, contentId: {contentId}, blogPostId: {blogPostId}"); } } context.Response.StatusCode = (int)HttpStatusCode.OK; } catch (Exception ex) { System.Console.WriteLine(ex.ToString()); context.Response.StatusCode = (int)HttpStatusCode.Conflict; } }); app.Run(); } private static string CalculateSignature(string data, string secret) { var encoding = new System.Text.UTF8Encoding(); byte[] secretByte = encoding.GetBytes(secret); HMACSHA256 hmacsha256 = new HMACSHA256(secretByte); byte[] dataBytes = encoding.GetBytes(data); byte[] dataHash = hmacsha256.ComputeHash(dataBytes); return Convert.ToBase64String(dataHash); } } public class WebhookEvents { public List events { get; set; } } public class WebhookEvent { public string TypeId { get; set; } public string DateOccurred { get; set; } public Dictionary EventData { get; set; } } }