我試圖將此PHP cookie解析片段轉換爲C#,但我的PHP有點生疏。它取自facebook SDK sample。什麼是這個PHP cookie解析片段的C#等價物?
<?php
define('FACEBOOK_APP_ID', 'your application id');
define('FACEBOOK_SECRET', 'your application secret');
function get_facebook_cookie($app_id, $application_secret) {
$args = array();
parse_str(trim($_COOKIE['fbs_' . $app_id], '\\"'), $args);
ksort($args);
$payload = '';
foreach ($args as $key => $value) {
if ($key != 'sig') {
$payload .= $key . '=' . $value;
}
}
if (md5($payload . $application_secret) != $args['sig']) {
return null;
}
return $args;
}
$cookie = get_facebook_cookie(FACEBOOK_APP_ID, FACEBOOK_SECRET);
echo 'The ID of the current user is ' . $cookie['uid'];
?>
這是我到目前爲止,但它並不完全正確:
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
HttpCookie cookie = GetCookie();
IsLoggedIn = cookie != null;
}
private HttpCookie GetCookie()
{
// based on the php example at http://developers.facebook.com/docs/authentication/
HttpCookie cookie = Request.Cookies["fbs_" + FacebookClientId];
StringBuilder payload = new StringBuilder();
if (cookie != null)
{
foreach (string key in cookie.Values.Keys)
{
if (key != "sig")
{
payload.Append(key + "=" + cookie.Values[key]);
}
}
string sig = cookie.Values["sig"];
if (sig == GetMD5Hash(payload.ToString()))
{
return cookie;
}
}
return null;
}
public string GetMD5Hash(string input)
{
MD5CryptoServiceProvider cryptoServiceProvider = new MD5CryptoServiceProvider();
byte[] bytes = Encoding.UTF8.GetBytes(input);
bytes = cryptoServiceProvider.ComputeHash(bytes);
StringBuilder s = new StringBuilder();
foreach (byte b in bytes)
{
s.Append(b.ToString("x2").ToLower());
}
return s.ToString();
}
的一個部分,我不知道是parse_str(trim($_COOKIE['fbs_' . $app_id], '\\"'), $args);
。從我可以告訴它從修剪後的cookie值中創建一個數組。任何人都可以提供幫助嗎?
這可能會幫助:http://php.net/manual/en/function.parse-str.php它解碼查詢字符串,並增加了鍵/值對組合。 – JAL 2010-07-27 02:56:55
因此,它假設每個鍵/值對由'&'分隔?那很有意思。我會看到我可以做什麼。 – jrummell 2010-07-27 03:02:01
好點 - 這對餅乾來說有點奇怪,不是嗎? – JAL 2010-07-27 03:34:14