Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
facebook webhook签名计算(C#)_C#_Facebook_Signature_Webhooks - Fatal编程技术网

facebook webhook签名计算(C#)

facebook webhook签名计算(C#),c#,facebook,signature,webhooks,C#,Facebook,Signature,Webhooks,FacebookWebhooks为每个调用提供一个签名,以验证有效负载的完整性和来源,但对我来说,它描述得很糟糕()。正因为如此,我在计算签名以验证它时遇到了一些问题。那么,计算值的步骤是什么呢?我让它工作起来了,并想在这里为其他开发人员分享我的解决方案(用C#表示): /// <summary> /// The HTTP request will contain an X-Hub-Signature header which contains the SHA1 si

FacebookWebhooks为每个调用提供一个签名,以验证有效负载的完整性和来源,但对我来说,它描述得很糟糕()。正因为如此,我在计算签名以验证它时遇到了一些问题。那么,计算值的步骤是什么呢?

我让它工作起来了,并想在这里为其他开发人员分享我的解决方案(用C#表示):

    /// <summary>
    /// The HTTP request will contain an X-Hub-Signature header which contains the SHA1 signature of the request payload,
    /// using the app secret as the key, and prefixed with sha1=.
    /// Your callback endpoint can verify this signature to validate the integrity and origin of the payload
    /// </summary>
    /// <param name="appSecret">facebook app secret</param>
    /// <param name="payload">body of webhook post request</param>
    /// <returns>calculated signature</returns>
    public static string CalculateSignature(string appSecret, string payload)
    {
        /*
         Please note that the calculation is made on the escaped unicode version of the payload, with lower case hex digits.
         If you just calculate against the decoded bytes, you will end up with a different signature.
         For example, the string äöå should be escaped to \u00e4\u00f6\u00e5.
         */
        payload = EncodeNonAsciiCharacters(payload);

        byte[] secretKey = Encoding.UTF8.GetBytes(appSecret);
        HMACSHA1 hmac = new HMACSHA1(secretKey);
        hmac.Initialize();
        byte[] bytes = Encoding.UTF8.GetBytes(payload);
        byte[] rawHmac = hmac.ComputeHash(bytes);

        return ByteArrayToString(rawHmac).ToLower();
    }

    private static string EncodeNonAsciiCharacters(string value)
    {
        StringBuilder sb = new StringBuilder();
        foreach (char c in value)
        {
            if (c > 127)
            {
                string encodedValue = "\\u" + ((int)c).ToString("x4");
                sb.Append(encodedValue);
            }
            else
            {
                sb.Append(c);
            }
        }
        return sb.ToString();
    }

    private static string ByteArrayToString(byte[] ba)
    {
        string hex = BitConverter.ToString(ba);
        return hex.Replace("-", "");
    }
//
///HTTP请求将包含一个X-Hub-Signature头,其中包含请求有效负载的SHA1签名,
///使用appsecret作为密钥,前缀为sha1=。
///回调端点可以验证此签名,以验证有效负载的完整性和来源
/// 
///facebook应用程序机密
///webhook post请求的主体
///计算签名
公共静态字符串CalculateSignature(字符串appSecret、字符串有效负载)
{
/*
请注意,计算是在有效负载的转义unicode版本上进行的,带有小写十六进制数字。
如果您只是根据解码的字节进行计算,您将得到一个不同的签名。
例如,字符串äöå应该转义到\u00e4\u00f6\u00e5。
*/
有效载荷=编码字符(有效载荷);
字节[]secretKey=Encoding.UTF8.GetBytes(appSecret);
HMACSHA1 hmac=新的HMACSHA1(保密密钥);
初始化();
byte[]bytes=Encoding.UTF8.GetBytes(有效负载);
字节[]rawHmac=hmac.ComputeHash(字节);
返回ByteArrayToString(rawHmac.ToLower();
}
私有静态字符串编码非字符(字符串值)
{
StringBuilder sb=新的StringBuilder();
foreach(字符c值)
{
如果(c>127)
{
字符串encodedValue=“\\u”+((int)c).ToString(“x4”);
某人附加(编码值);
}
其他的
{
sb.附加(c);
}
}
使某人返回字符串();
}
私有静态字符串ByteArrayToString(字节[]ba)
{
字符串十六进制=位转换器.ToString(ba);
返回十六进制。替换(“-”,”);
}

分享很好,但你应该坚持问答的“格式”。因此,请将您的问题编辑为描述问题的内容,然后将您的解决方案发布为自我回答@我为此道歉。我已经修好了。谢谢