0
它是2010年9月,我試圖在我們的網站上更新Facebook連接集成。Facebook簽名驗證,.NET代碼
我需要驗證由Facebook代碼設置的auth cookie。我從以下http://developers.facebook.com/docs/guides/web
PHP示例請檢查C#代碼:
public static Dictionary<string, string> ParseCookie(HttpCookie fbCookie)
{
if (fbCookie == null)
return null;
string value = fbCookie.Value.Substring(1, fbCookie.Value.Length - 2);
SortedDictionary<string, string> sargs = new SortedDictionary<string, string>();
foreach (string pair in value.Split('&'))
{
string[] keyvalue = pair.Split('=');
sargs.Add(keyvalue[0], keyvalue[1]);
}
string sid = sargs["sig"] ?? string.Empty;
sargs.Remove("sig");
string payload = string.Empty;
foreach (KeyValuePair<string, string> pair in sargs)
{
payload += pair.Key + "=" + pair.Value;
}
if (string.IsNullOrEmpty(payload) || DataFormatter.GetMD5Hash(payload + Settings.ApplicationSecret).ToUpper() != sid.ToUpper())
return null;
return sargs.ToDictionary(pair => pair.Key, pair => pair.Value);
}
DataFormatter.GetMD5Hash方法是:
public static string GetMD5Hash(string key)
{
StringBuilder result = new StringBuilder();
MD5 md5 = new MD5CryptoServiceProvider();
foreach (byte b in md5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key)))
result.Append(b.ToString("X2"));
return result.ToString();
}
的問題是,我的MD5永遠不匹配來自Facebook SIG的。
DataFormatter.GetMD5Hash(payload + Settings.ApplicationSecret).ToUpper() != sid.ToUpper()
始終是真實的
請大家幫忙找到解決方案。
感謝