0

我有错误

你调用的对象是空的

在:

string[] sB64String = payload.Split('.');

当检查用户是否喜欢我的 Facebook 页面时,我的代码 -

protected void Page_Load(object sender, EventArgs e)
{
    pageLike();
}

public bool ValidateSignedRequest()
{
    string facebooksecret =
    System.Configuration.ConfigurationManager.AppSettings["FacebookSecret"];
    var VALID_SIGNED_REQUEST = Request.Form["signed_request"];

    string applicationSecret = facebooksecret;
    string[] signedRequest = VALID_SIGNED_REQUEST.Split('.');
    string expectedSignature = signedRequest[0];
    string payload = signedRequest[1];

    // Attempt to get same hash
    var Hmac = SignWithHmac(UTF8Encoding.UTF8.GetBytes(payload), UTF8Encoding.UTF8.GetBytes(applicationSecret));
    var HmacBase64 = ToUrlBase64String(Hmac);

    return (HmacBase64 == expectedSignature);
}

private string ToUrlBase64String(byte[] Input)
{
    return Convert.ToBase64String(Input).Replace("=", String.Empty)
                                        .Replace('+', '-')
                                        .Replace('/', '_');
}

private byte[] SignWithHmac(byte[] dataToSign, byte[] keyBody)
{
    using (var hmacAlgorithm = new HMACSHA256(keyBody))
    {
        hmacAlgorithm.ComputeHash(dataToSign);
        return hmacAlgorithm.Hash;
    }
}
public Dictionary<string, string> DecodePayload(string payload)
{
    //Remove the bad part of signed_request
    //Begin
    string[] sB64String = payload.Split('.');
    payload = payload.Replace((sB64String[0] + "."), string.Empty);
    //End
    var encoding = new UTF8Encoding();
    var decodedJson = payload.Replace("=", string.Empty).Replace('-', '+').Replace('_', '/');
    var base64JsonArray = Convert.FromBase64String(decodedJson.PadRight(decodedJson.Length + (4 - decodedJson.Length % 4) % 4, '='));
    var json = encoding.GetString(base64JsonArray);
    var jObject = JObject.Parse(json);
    var parameters = new Dictionary<string, string>();

    parameters.Add("page", ((bool)jObject["page"]["liked"]).ToString());
    parameters.Add("admin", ((bool)jObject["page"]["admin"]).ToString());
    return parameters;
}
protected void pageLike()
{
    string pageLiked = string.Empty;
    var signed_request = Request.Form["signed_request"];
    var json = DecodePayload(signed_request);


    foreach (KeyValuePair<string, string> objKVP in json)
    {
        //Note You can also see if a user is an admin by replacing the objKVP.Key with admin
        if (objKVP.Key == "page" && objKVP.Value == "True")
        {
            Response.Redirect("https://facebookapp.elarabygroup.com/instruction.aspx");
            //litJson.Text += objKVP.Key + " - " + objKVP.Value + "<br />";
        }
    }
}
4

1 回答 1

0

我看不出您的代码有任何根本错误,我怀疑正在发生的事情是请求表单变量为空,即

var signed_request = Request.Form["signed_request"];

有效负载变量被设置为 null 或空字符串。我会检查这个并确保该值是您所期望的。

这里的一个好主意是在 DecodePayload 中添加一个保护子句,以确保在调用 Split 方法之前有效负载变量具有一个值。

例如

if (string.IsNullOrEmpty(payload))
    throw new ArgumentNullException();
于 2013-09-07T19:04:33.373 回答