检查FB连接时,为什么HttpContext.Current.Request.Cookies [fullCookie]始终为NULL?

时间:2012-08-17 03:28:59

标签: c# asp.net .net facebook facebook-graph-api

我是使用FB API的新手,我正试图从我的Asp.net应用程序发布到Facebook墙。

我从FB获得了Appkey和密钥,只是想跟进 在FB墙上发布的代码。

LINK:http://kennetham.com/2010/07/21/facebook-api-asp-net/

我现在面临的问题是,在我的ConnectAuthentication类中,HttpContext.Current.Request.Cookies [fullCookie]始终为NULL。因此,当我在我的页面加载中通过“if(ConnectAuthentication.isConnected())”检查FB连接时,它总是返回false并且它不会在条件内运行代码。

为什么?我错过了什么吗?

ConnectAuthentication类

public class ConnectAuthentication
{
    public ConnectAuthentication()
    {

    }

    public static bool isConnected()
    {
        return (SessionKey != null && UserID != -1);
    }

    public static string ApiKey
    {
        get
        {
            return ConfigurationManager.AppSettings["APIKey"];
        }
    }

    public static string SecretKey
    {
        get
        {
            return ConfigurationManager.AppSettings["Secret"];
        }
    }

    public static string SessionKey
    {
        get
        {
            return GetFacebookCookie("session_key");
        }
    }

    public static long UserID
    {
        get
        {
            long userID = -1;
            long.TryParse(GetFacebookCookie("user"), out userID);
            return userID;
        }
    } 
    private static string GetFacebookCookie(string cookieName)
    {
        string retString = null;
        string fullCookie = ApiKey + "_" + cookieName; 

        if (HttpContext.Current.Request.Cookies[fullCookie] != null)
            retString = HttpContext.Current.Request.Cookies[fullCookie].Value;

        return retString;
    }
}

以下是我的页面加载中使用ConnectAuthentication类的方法:

    if (ConnectAuthentication.isConnected())
                {
                    Facebook.Session.ConnectSession session = new Facebook.Session.ConnectSession(ConnectAuthentication.ApiKey, ConnectAuthentication.SecretKey);

                    _connectSession = new ConnectSession(ConnectAuthentication.ApiKey, ConnectAuthentication.SecretKey);

                    Api _facebookAPI = new Api(_connectSession);

                    _connectSession.UserId = ConnectAuthentication.UserID;
                    Facebook.Rest.Api api = new Facebook.Rest.Api(_connectSession);

                    //Display user data captured from the Facebook API.

                    Facebook.Schema.user user = api.Users.GetInfo();
                    string fullName = user.first_name + " " + user.last_name;
                    Panel1.Visible = true;
                    Label1.Text = fullName;

                }
                else
                {
                    //Facebook Connect not authenticated, proceed as usual.
                }
           }

1 个答案:

答案 0 :(得分:0)

这段代码完美无缺......

<input type="button" id="fblogin" value="Login to Facebook" disabled="disabled" style="display:none"/>
        <fb:login-button v="2" length="long" onlogin="window.location = 'Default.aspx'">Login to Facebook</fb:login-button>

<div id="fb-root"></div>
<script>
    window.fbAsyncInit = function () {
        FB.init({
            appId: '<%: Facebook.FacebookApplication.Current.AppId %>',
            cookie: true,
            xfbml: true,
            oauth: true
        });

        function facebooklogin() {
            FB.login(function (response) {
                if (response.authResponse) {
                    // user authorized
                    // make sure to set the top.location instead of using window.location.reload()
                    top.location = '<%= this.ResolveCanvasPageUrl("~/") %>';
                } else {
                    // user cancelled
                }
            }, { scope: '<%: string.Join(",", ExtendedPermissions) %>' });
        };

        $(function () {
            // make the button is only enabled after the facebook js sdk has been loaded.
            $('#fblogin').attr('disabled', false).click(facebooklogin);
        });
    };
    (function () {
        var e = document.createElement('script'); e.async = true;
        e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
        document.getElementById('fb-root').appendChild(e);
    } ());
</script>