错误在检查Facebook时,对象引用未设置为对象的实例?

时间:2013-09-07 18:57:32

标签: c# asp.net facebook

我有错误

  

对象引用未设置为对象的实例

在:

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 />";
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我看不出任何根本错误的代码,我怀疑发生的是请求表单变量是否为空。

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

有效负载变量被设置为空字符串或空字符串。我检查一下,确保价值符合您的预期。

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

e.g。

if (string.IsNullOrEmpty(payload))
    throw new ArgumentNullException();