Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.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 AppSecret证明HMACSHA256所需的C#帮助_C#_Asp.net_Facebook_Facebook C# Sdk_Hmac - Fatal编程技术网

创建Facebook AppSecret证明HMACSHA256所需的C#帮助

创建Facebook AppSecret证明HMACSHA256所需的C#帮助,c#,asp.net,facebook,facebook-c#-sdk,hmac,C#,Asp.net,Facebook,Facebook C# Sdk,Hmac,Facebook要求我创建appsecret\u证明: 我使用以下代码完成了这项工作: public string FaceBookSecret(string content, string key) { var encoding = new System.Text.ASCIIEncoding(); byte[] keyByte = encoding.GetBytes(key); byte[] messageBytes = encoding.Get

Facebook要求我创建appsecret\u证明:

我使用以下代码完成了这项工作:

public string FaceBookSecret(string content, string key)
{
        var encoding = new System.Text.ASCIIEncoding();
        byte[] keyByte = encoding.GetBytes(key);
        byte[] messageBytes = encoding.GetBytes(content);
        using (var hmacsha256 = new HMACSHA256(keyByte))
        {
            byte[] hashmessage = hmacsha256.ComputeHash(messageBytes);
            return Convert.ToBase64String(hashmessage);
        }
}
对我来说一切都很好,但是facebook说appsecret_证明无效。我已登录,当我拔出钥匙时,我可以做一切正常的事情。因此,为了节省一些时间:

  • 是的,我正在发布到正确的URL
  • 是,我正在传递有效的访问令牌
  • 是的,我在证明中使用了与请求中相同的访问令牌
  • 是的,我的appsecret很好,很有效
使用中的示例

dynamic results = client.Post("/" + model.PostAsId + "/feed", new { message = model.Message, appsecret_proof = FaceBookSecret(postAs.AuthToken, AppSecret) });
我认为这可能与编码或类似的东西有关,但老实说,我只是不知道

我也在使用Facebook.NETSDK,但是这并没有太多文档,而且似乎与自动化、服务器端操作等没有任何关系


谢谢

应用程序机密是一个base-16字符串,因此您需要将其转换为字节数组。请参阅,了解有关如何执行此操作的详细信息。需要使用ASCII编码将access_令牌转换为字节数组。生成HMAC后,将其编码为base-16字符串,用作appsecret\u证明。下面的代码将字节数组转换为base16

public static class Base16
{
    private static readonly char[] encoding;

    static Base16()
    {
        encoding = new char[16]
        {
            '0', '1', '2', '3', '4', '5', '6', '7',
            '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
        };
    }

    public static string Encode(byte[] data)
    {
        char[] text = new char[data.Length * 2];

        for (int i = 0, j = 0; i < data.Length; i++)
        {
            text[j++] = encoding[data[i] >> 4];
            text[j++] = encoding[data[i] & 0xf];
        }

        return new string(text);
    }
}

Facebook似乎接受了一个
SHA256 HMAC
SHA1 HMAC

我已经在Facebook上成功地使用了以下内容

using System.Security.Cryptography;
using System.Text;

internal static string FaceBookSecret(string content, string key)
{
    byte[] keyBytes = Encoding.UTF8.GetBytes(key);
    byte[] messageBytes = Encoding.UTF8.GetBytes(content);
    byte[] hash;
    using (HMACSHA256 hmacsha256 = new HMACSHA256(keyBytes))
    {
        hash = hmacsha256.ComputeHash(messageBytes);
    }

    StringBuilder sbHash = new StringBuilder();
    for (int i = 0; i < hash.Length; i++)
    {
        sbHash.Append(hash[i].ToString("x2"));
    }
    return sbHash.ToString();
}
使用System.Security.Cryptography;
使用系统文本;
内部静态字符串FaceBookSecret(字符串内容、字符串键)
{
byte[]keyBytes=Encoding.UTF8.GetBytes(键);
byte[]messageBytes=Encoding.UTF8.GetBytes(内容);
字节[]散列;
使用(HMACSHA256 HMACSHA256=新的HMACSHA256(键字节))
{
hash=hmacsha256.ComputeHash(messageBytes);
}
StringBuilder sbHash=新的StringBuilder();
for(int i=0;i
能否显示更多代码?如何创建
客户端
?您确定对哈希和请求使用相同的app.Id吗?另外-请尝试
Encoding.UTF8.GetBytes
以防万一-可能这会起到作用…我也遇到了同样的问题,并在这里为facebook API v2.4解决了它:这个答案缺少Base16.Decode实现。我们是不是应该自己来解决这个问题?让我惊讶的是,这是一个被接受的答案,而不是Eric Kassan给出的答案,它是完整的、正确的,没有太多的胡塞尔。我没有包括Base16.Decode的代码,因为在我链接的问题(以及相关的副本)中已经有很多解决方案。Eric Kassan解决方案本身就很有趣,因为他只是使用UTF8.GetBytes来解码appSecret。这意味着Facebook会使用多种不同的算法验证appsecret_证明。对于未来的用户,请注意小写的“x2”很重要,否则您将收到400个错误请求。
using System.Security.Cryptography;
using System.Text;

internal static string FaceBookSecret(string content, string key)
{
    byte[] keyBytes = Encoding.UTF8.GetBytes(key);
    byte[] messageBytes = Encoding.UTF8.GetBytes(content);
    byte[] hash;
    using (HMACSHA256 hmacsha256 = new HMACSHA256(keyBytes))
    {
        hash = hmacsha256.ComputeHash(messageBytes);
    }

    StringBuilder sbHash = new StringBuilder();
    for (int i = 0; i < hash.Length; i++)
    {
        sbHash.Append(hash[i].ToString("x2"));
    }
    return sbHash.ToString();
}