Search code examples
c#facebookfacebook-graph-apiasp.net-web-apisha1

Expected X-Hub-Signature via SHA1 not same as what FB is sending (code in C#)


I am having problems getting the X-Hub-Signature sent to me by facebook to match the one I am generating in C#. For a while I thought I was running the function wrong but I have now used multiple code sources on Stack Overflow and a website (http://billatnapier.com/security01.aspx) to confirm I am indeed creating the SHA-1 correctly.

So .... something is clearly wrong with the content. I am using ASP.NET Web API and the "Payload" that I am using to feed into the SHA-1 algorithm is the JSON object I am receiving from facebook, converted to a string. I assume this is what they want me to use when they say "Payload" is that correct? It is a string that begins with {"entry":[ and ends with "object":"page"}

I feel like I've tried everything and have hit a brick wall so hoping someone can help me. Web API is a bit off - even grabbing the X-Hub-Signature was a challenge as you can't just use Request.Header["X-Hub-Signature"]; I am almost wondering if I should switch back to pure MVC.


Solution

  • OK so I am answering my own question! The problem with the "Payload" is that you can't simply grab the JSON object. You have to find a way to access the Request object from Web API and then read in the payload like this:

    var context = Request.Properties["MS_HttpContext"] as HttpContextWrapper; 
    
    using (StreamReader reader = new StreamReader(context.Request.InputStream)) 
    { 
    payload = reader.ReadToEnd(); 
    } 
    

    It looks like binning Web API and just doing this in MVC would be easier as you then just do this:

    using (StreamReader reader = new StreamReader(HttpContext.Request.InputStream))
    {
        PayLoad = reader.ReadToEnd();
    }