开发者

Null Session with Facebook C# SDK 4.0.2

开发者 https://www.devze.com 2023-02-27 20:52 出处:网络
I\'m working with the Facbook C# SDK v4.0.2 because that is what is included in Telerik\'s Sitefinity CMS. I tried updating but Sitefinity complains.

I'm working with the Facbook C# SDK v4.0.2 because that is what is included in Telerik's Sitefinity CMS. I tried updating but Sitefinity complains.

Does anyone know if v4.0.2 is still valid? I logged in using the FB JS SDK and the <fb:login-button/> and I'm able to query the API via JavaScript. When I try it with the C# SDK I always get (new FacebookApp).Session == null. I'm using the SDK in a webforms app but the example I am going off of is from the SDK's MVC example.

SDK M开发者_StackOverflowVC example:

    public ActionResult Profile()
    {
        var app = new FacebookApp();
        if (app.Session == null)
        {
            // The user isnt logged in to Facebook
            // send them to the home page
            return RedirectToAction("Index");
        }

        // more example code ...
    }

I am doing something similar:

    var app = new FacebookApp();
    if (app.Session != null)    
    {
        FormsAuthentication.SetAuthCookie(user.UserName, false);    
    }
    else
    {
        //user is logged in to facebook but has not registered.
        response.Redirect("register.aspx?needsFacebook=1", true);
    }

Is v.4.0.2 still valid? Is this the way that I should be checking on the server if the user is authenticated with Facebook?


I would definitely try to update to version 5. On the one hand you might have some small problems upgrading but you avoid bigger problems working with an old version.


I can't tell if you are using winforms or asp.net, either way try using this API instead and see if it fixes your issue. It should only take you a few minutes to figure out if it works.

To authenticate you would do the following for winforms:

This will authenticate the user when a button is clicked on the form.

Imports Branches.FBAPI
...
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim SI As New SessionInfo("[application_id]","applicaiton_secret")
     ' Add an event handler so that you can process
     ‘ the response when the user is authenticated
     AddHandler SI.AuthenticationResult, AddressOf ReadResponse
     ' Call the function to display a form to the user and log them into Facebook
     SI.AuthenticateUser(Me, False)
End Sub

When the process is complete an event call ReadResponse is fired and contains the same response object as in all other authentication steps.

Sub ReadResponse(ByVal FBR As Branches.FBAPI.SessionInfo.FacebookResponse)
     Label1.Text &= " - " & FBR.UserID
     Me.BringToFront()
End Sub


I ended up downloading the source for the 4.2 SDK, changing the assembly version in AssemblyInfo.cs to version 4.0.2 and recompiling. Thus, I tricked Sitefinity into thinking it was using the 4.0.2 SDK when it was really using the 4.2. This totally sidestepped my original question but at least everything is working now.

0

精彩评论

暂无评论...
验证码 换一张
取 消