tags:

views:

581

answers:

1

I'm trying to work with Facebook in my ASP.NET site where I have a logged in user of my site enter in text in a box for their status on my site. I want them to be able to push that status to FB as well per their permission. I did it using simple Facebook Connect JS code but I want to get the info in .NET and push it that way. I'm not actually creating a FB app for a FB profile though.

This is pseudo-code for what I want:

Facebook fb = new Facebook(apiKey);
FBSession sess = fb.Authenticate();
if(sess.isAuthenticated) {
  User u = fb.getUser(sess.userId);
  u.setStatus(Textbox1.text);
  u.SaveStatus();
}

Does anything like this even exist for .NET or is their API for PHP only?

+1  A: 

Sure, here is some sample code. It's using the Facebook Developer Toolkit (you can find it on codeplex). And FBConnectAuth which can also be found on codeplex.

You want to check the cookies to make sure that the cookie is real (making sure someone is not trying to hack in); which is why the cookie validation step is important.

As long as you login using the JS code, the same cookies that are set in the JS are accessible in C#.

This works fine for a Facebook Connect app.

        using FBConnectAuth;
        using facebook;

        public facebook.API api = new facebook.API();

        bool IsLoggedInToFacebook = false;
        FBConnectAuthentication auth = new FBConnectAuthentication(ConfigurationManager.AppSettings["AppKey"], ConfigurationManager.AppSettings["Secret"]);
        if (auth.Validate() != ValidationState.Valid)
        {
            IsLoggedInToFacebook = false;
        }
        else
        {
            FBConnectSession fbSession = auth.GetSession();
            string userId = fbSession.UserID;
            string sessionKey = fbSession.SessionKey;

            api.ApplicationKey = ConfigurationManager.AppSettings["AppKey"];
            api.SessionKey = sessionKey;
            api.Secret = ConfigurationManager.AppSettings["Secret"];
            api.uid = Convert.ToInt64(userId);
            api.status.set("statutes text goes here")

            IsLoggedInToFacebook = true;
        }
rksprst