using System; using System.Net; using System.Security.Cryptography; using System.Web; using System.Web.Mvc; using System.Web.Script.Serialization; namespace SampleWebhookHandler.Controllers { public class HomeController : Controller { // // GET: /Home/ public ActionResult Index() { return View(); } private const string SECRET_KEY = "arsuwuxsy2lesiq7a835gdttu39"; [HttpPost] [AllowAnonymous] [ActionName("Index")] public ActionResult Callback() { try { string senderUrl = HttpContext.Request.Headers["X-Telligent-Webhook-Sender"]; string hashSignature = HttpContext.Request.Headers["X-Telligent-Webhook-Signature"]; string rawPostData; using (var reader = new System.IO.StreamReader(HttpContext.Request.InputStream)) { rawPostData = reader.ReadToEnd(); } // 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. return new HttpStatusCodeResult(HttpStatusCode.BadRequest); dynamic json = new JavaScriptSerializer().DeserializeObject(rawPostData); foreach (var webhookEvent in json["events"]) { var typeId = webhookEvent["TypeId"].ToString(); // Ensure that the event type is the one you want to handle if (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()); // do something with the data } } return new HttpStatusCodeResult(200); } catch (Exception ex) { return new HttpStatusCodeResult(HttpStatusCode.Conflict, ex.ToString()); } } private 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); } } }