Null Session with Facebook C# SDK 4.0.2

本文关键字:SDK Session with Facebook Null | 更新日期: 2023-09-27 17:52:42

我正在使用facebook c# SDK v4.0.2,因为这是Telerik的Sitefinity CMS中包含的内容。我尝试更新,但Sitefinity抱怨。

有谁知道v4.0.2是否仍然有效?我使用FB JS SDK和<fb:login-button/>登录,我可以通过JavaScript查询API。当我尝试使用c# SDK时,我总是得到(new FacebookApp).Session == null。我在webforms应用程序中使用SDK,但我要离开的示例来自SDK的MVC示例。

SDK MVC示例:

    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 ...
    }

我正在做类似的事情:

    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);
    }

v4.0.2仍然有效吗?如果用户通过Facebook身份验证,我应该在服务器上检查这种方式吗?

Null Session with Facebook C# SDK 4.0.2

我肯定会尝试更新到版本5。一方面,你可能在升级时遇到一些小问题,但你可以避免使用旧版本时出现更大的问题。

我不知道你是使用winforms还是asp.net,无论哪种方式尝试使用这个API,看看它是否解决了你的问题。你应该只需要几分钟就能知道它是否有效。

要对winforms进行身份验证,您需要执行以下操作:

这将在表单上单击按钮时验证用户。

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

当流程完成时,触发一个事件调用ReadResponse,并包含与所有其他身份验证步骤相同的响应对象。

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

我最终下载了4.2 SDK的源代码,将AssemblyInfo.cs中的汇编版本更改为4.0.2版本并重新编译。因此,我欺骗了Sitefinity,让它以为它在使用4.0.2 SDK,而实际上它使用的是4.2。这完全回避了我最初的问题,但至少现在一切都正常了。