【问题标题】:Facebook authentication script from ASP to PHP从 ASP 到 PHP 的 Facebook 身份验证脚本
【发布时间】:2009-11-24 16:43:16
【问题描述】:

我正在尝试将下面的代码转换为 php 版本,如果有人可以提供帮助,谢谢。

private bool IsValidFacebookSignature()
    {
        //keys must remain in alphabetical order
        string[] keyArray = { "expires", "session_key", "ss", "user" };
        string signature = "";

        foreach (string key in keyArray)
            signature += string.Format("{0}={1}", key, GetFacebookCookie(key));

        signature += SecretKey; //your secret key issued by FB

        MD5 md5 = MD5.Create();
        byte[] hash = md5.ComputeHash(Encoding.UTF8.GetBytes(signature.Trim()));

        StringBuilder sb = new StringBuilder();
        foreach (byte hashByte in hash)
            sb.Append(hashByte.ToString("x2", CultureInfo.InvariantCulture));

        return (GetFacebookCookie("") == sb.ToString());
    }

    private string GetFacebookCookie(string cookieName)
    {
        //APIKey issued by FB
        string fullCookie = string.IsNullOrEmpty(cookieName) ? ApiKey : ApiKey + "_" + cookieName;

        return Request.Cookies[fullCookie].Value;
    }

【问题讨论】:

    标签: php authentication facebook signature


    【解决方案1】:

    它未经测试,但试试这个:

    function IsValidFacebookSignature() {
        $keyArray = array( 'expires', 'session_key', 'ss', 'user' );
        $signature = '';
    
        foreach( $key in $keyArray ) {
            $signature .= "$key=".GetFacebookCookie($key);
        }
    
        $signature .= $SecretKey;
        $hash = md5(trim($signature));
    
        return GetFacebookCookie('') == $hash;
    }
    
    function GetFacebookCookie($cookieName) {
        $fullCookie = empty($cookie) ? $APIKey : $APIKey . '_' . $cookieName;
        return $_COOKIES[$fullCookie];
    }
    

    我不确定您希望在哪里声明 $SecretKey$APIKey,但这是基本想法。

    【讨论】:

    • 酷,谢谢。不幸的是,它不起作用 - 我已经放弃了这种方法并采用了完全不同的方法,但感谢您的努力。
    猜你喜欢
    • 2011-07-09
    • 2012-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-24
    • 1970-01-01
    • 2011-04-06
    • 1970-01-01
    相关资源
    最近更新 更多