c# - How do I receive a custom webhook in an IIS hosted website? -
here have done:
1 - have installed nuget package: microsoft.aspnet.webhooks.receivers.custom 1.2.0-beta
2 - configured webapiconfig receive custom webhooks:
    public static void register(httpconfiguration config)     {         // web api configuration , services          // web api routes         config.maphttpattributeroutes();          config.routes.maphttproute(             name: "defaultapi",             routetemplate: "api/{controller}/{id}",             defaults: new { id = routeparameter.optional }         );          config.initializereceivecustomwebhooks(); //<<<---     }   3 - set secret key in web.config:
  <appsettings>     <add key="webpages:version" value="3.0.0.0" />     ...     <add key="ms_webhookreceiversecret_genericjson" value="z=secret"/>    </appsettings>   4 - have written basic receiver (with genericjson hook capture)
public class genericjsonwebhookhandler : webhookhandler {     public static string datareceived;     public genericjsonwebhookhandler()     {         this.receiver = "genericjson";     }      public override task executeasync(string generator, webhookhandlercontext context)     {         // json webhook         jobject data = context.getdataordefault<jobject>();          if (context.id == "i")         {             // stuff         }         else if (context.id == "z")         {             // more stuff             datareceived = data.tostring();              file.create(@"c:\test\test1.txt");         }          return task.fromresult(true);     } }   now, 1 expect steps above, if webhook sender set publish json iis hosted site, should capture notification json, assign captured data datareceived , write blank text file c:\test\test.txt - which not case
currently, testing using team foundation server send webhook test https://mywebbhooksite.com:5050/?z=secret, , succeeds - however, when check if little test file has been created, it's not there. have javascript running on homepage poll changes datareceived see nothing happening.
mentioning here: have remote debugger attached w3wp.exe process, breakpoint on executeasync , genericjsonwebhookhandler not hit
are there other specific setup needs done in order webhook captured?
i took filthy approach works
i ditched genericjsonwebhookhandler , instead have utilized application_beginrequest() event in webapiapplication instead intercept data posted sender webhook. body of hook resides in httprequest.request.inputstream, can opened using streamreader. contents can read onto string , parsed jobject (if body of request sent webhook request json)
here code.
    protected void application_beginrequest()     {         if (!request.httpmethod.equals("post", stringcomparison.invariantcultureignorecase)) {             return;          }          string documentcontents;         using (var receivestream = request.inputstream)         {             using (var readstream = new streamreader(receivestream, encoding.utf8))             {                 documentcontents = readstream.readtoend();             }         }          try         {             var json = jobject.parse(documentcontents);             file.writealllines(@"c:\test\keys.txt", new[] { documentcontents, "\r\n", json.tostring() });         }         catch (exception)         {              //         }     }   the test:
i went onto webhook , commenced webhook test. published request json. http 200 response server.
breakpoint hit. httpmethod picked post. request's inputstream read , stored in documentcontents. jobject.parse fired off , put contents of post jobject variable called json
the contents of json written file stored on server - indicating request received.
what plan improve this, security
for security purposes, encrypt secret key set in web.config, , set encrypted key in web.config instead, , after match incoming url query parameters (using same encryption algorithm) see if key present , same
Comments
Post a Comment