Click here to Skip to main content
15,997,509 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am totally new in using FB API and i am trying to post to facebook wall from my Asp.net application.

I have got the Appkey and secret key from FB and just trying to follow
the code to post in FB wall.

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

The problem i am facing now is, in my ConnectAuthentication Class, HttpContext.Current.Request.Cookies[fullCookie] is always NULL. Due to that, when i check for the FB connectivity by "if (ConnectAuthentication.isConnected())" in my pageload, it always returns false and it does not run the code inside condition.

Why is that? Am i missing something ?

ConnectAuthentication Class

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;
    }
}
Here is how the ConnectAuthentication Class is used in my page load :

    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.
                }
           }
Posted
Updated 19-Dec-12 19:24pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900