In this article I will explain how to logout of FaceBook Website in ASP.Net using the Free ASPSnippets FaceBook API.
 
Note: You can download the latest ASPSnippets.FaceBookAPI.dll clicking the download link below.
          Download DLL file
 
 
 
Create FaceBook Application and get App Id
You will need to create an application and get an API Key and API Secret. You can refer my article
 
HTML Markup
In the below HTML Markup I have placed an ASP.Net Button btnLogin to authenticate the user through FaceBook and get its profile details like FaceBook UserId, Username, Name, Email, Profile picture and a button to logout from FaceBook website.
<asp:Button ID="btnLogin" runat="server" Text="Login with FaceBook" OnClick="Login" />
<asp:Panel ID="pnlFaceBookUser" runat="server" Visible="false">
    <hr />
    <table>
        <tr>
            <td rowspan="5" valign="top">
                <asp:Image ID="ProfileImage" runat="server" Width="50" Height="50" />
            </td>
        </tr>
        <tr>
            <td>
                ID:<asp:Label ID="lblId" runat="server" Text=""></asp:Label>
            </td>
        </tr>
        <tr>
            <td>
                UserName:<asp:Label ID="lblUserName" runat="server" Text=""></asp:Label>
            </td>
        </tr>
        <tr>
            <td>
                Name:<asp:Label ID="lblName" runat="server" Text=""></asp:Label>
            </td>
        </tr>
        <tr>
            <td>
                Email:<asp:Label ID="lblEmail" runat="server" Text=""></asp:Label>
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <asp:Button ID="btnLogout" runat="server" Text="Logout" OnClick="Logout" />
            </td>
        </tr>
    </table>
</asp:Panel>
 
 
 
Namespaces
You will need to import the following Namespaces
C#
using ASPSnippets.FaceBookAPI;
using System.Web.Script.Serialization;
 
VB.Net
Imports ASPSnippets.FaceBookAPI
Imports System.Web.Script.Serialization
 
 
Data Class
You will need to create the following class which will be used to hold the User Profile details returned from FaceBook after authentication.
C#
public class FaceBookUser
{
    public string Id { get; set; }
    public string Name { get; set; }
    public string UserName { get; set; }
    public string PictureUrl { get; set; }
    public string Email { get; set; }
}
 
VB.Net
Public Class FaceBookUser
    Public Property Id() As String
        Get
            Return m_Id
        End Get
        Set(value As String)
            m_Id = value
        End Set
    End Property
    Private m_Id As String
    Public Property Name() As String
        Get
            Return m_Name
        End Get
        Set(value As String)
            m_Name = value
        End Set
    End Property
    Private m_Name As String
    Public Property UserName() As String
        Get
            Return m_UserName
        End Get
        Set(value As String)
            m_UserName = value
        End Set
    End Property
    Private m_UserName As String
    Public Property PictureUrl() As String
        Get
            Return m_PictureUrl
        End Get
        Set(value As String)
            m_PictureUrl = value
        End Set
    End Property
    Private m_PictureUrl As String
    Public Property Email() As String
        Get
            Return m_Email
        End Get
        Set(value As String)
            m_Email = value
        End Set
    End Property
    Private m_Email As String
End Class
 
 
 
Authenticate user using FaceBook account
On the click of the ASP.Net Button btnLogin  we redirect the User to FaceBook and ask him to authenticate himself as wells as provide permission to access to his FaceBook profile details like FaceBook UserId, Username, Name, Email and Profile picture
C#
protected void Login(object sender, EventArgs e)
{
    FaceBookConnect.Authorize("user_photos,email", Request.Url.AbsoluteUri.Split('?')[0]);
}
 
VB.Net
Protected Sub Login(sender As Object, e As EventArgs)
    FaceBookConnect.Authorize("user_photos,email", Request.Url.AbsoluteUri.Split("?"c)(0))
End Sub
 
 
Fetch the user’s FaceBook Profile details
The below code checks for access code (access token) in Query string and then based on the access code it fetches the user’s  FaceBook profile details like FaceBook UserId, Username, Name, Email and Profile picture.
Also in the Page Load event I have handled the following two cases
1. Access Denied: - This allows us to determine whether user has denied access to the FaceBook App.
2. Logout: - This allows us to determine whether user has logged out from the FaceBook Website.
C#
protected void Page_Load(object sender, EventArgs e)
{
    FaceBookConnect.API_Key = "<Your API Key>";
    FaceBookConnect.API_Secret = "<Your API Secret>";
    if (!IsPostBack)
    {
        if (Request.QueryString["logout"] == "true")
        {
            ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('User has logged out from Facebook')", true);
            return;
        }
 
        if (Request.QueryString["error"] == "access_denied")
        {
            ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('User has denied access.')", true);
            return;
        }
 
        string code = Request.QueryString["code"];
        if (!string.IsNullOrEmpty(code))
        {
            string data = FaceBookConnect.Fetch(code, "me?fields=id,name,email");
            FaceBookUser faceBookUser = new JavaScriptSerializer().Deserialize<FaceBookUser>(data);
            faceBookUser.PictureUrl = string.Format("https://graph.facebook.com/{0}/picture", faceBookUser.Id);
            pnlFaceBookUser.Visible = true;
            lblId.Text = faceBookUser.Id;
            lblUserName.Text = faceBookUser.UserName;
            lblName.Text = faceBookUser.Name;
            lblEmail.Text = faceBookUser.Email;
            ProfileImage.ImageUrl = faceBookUser.PictureUrl;
            btnLogin.Enabled = false;
        }
    }
}
 
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    FaceBookConnect.API_Key = "<Your API Key>"
    FaceBookConnect.API_Secret = "<Your API Secret>"
    If Not IsPostBack Then
        If Request.QueryString("logout") = "true" Then
            ClientScript.RegisterStartupScript(Me.[GetType](), "alert", "alert('User has logged out from Facebook')", True)
            Return
        End If
 
        If Request.QueryString("error") = "access_denied" Then
            ClientScript.RegisterStartupScript(Me.[GetType](), "alert", "alert('User has denied access.')", True)
            Return
        End If
 
        Dim code As String = Request.QueryString("code")
        If Not String.IsNullOrEmpty(code) Then
            Dim data As String = FaceBookConnect.Fetch(code, "me?fields=id,name,email")
            Dim faceBookUser As FaceBookUser = New JavaScriptSerializer().Deserialize(Of FaceBookUser)(data)
            faceBookUser.PictureUrl = String.Format("https://graph.facebook.com/{0}/picture", faceBookUser.Id)
           pnlFaceBookUser.Visible = True
            lblId.Text = faceBookUser.Id
            lblUserName.Text = faceBookUser.UserName
            lblName.Text = faceBookUser.Name
            lblEmail.Text = faceBookUser.Email
            ProfileImage.ImageUrl = faceBookUser.PictureUrl
            btnLogin.Enabled = False
        End If
    End If
End Sub
 
 
 
Logout from FaceBook using Graph API
Below is the event handler which is raised when the Logout button is clicked, it simply makes call to the ASPSnippets FaceBook API Logout method and passes the FaceBook Graph API Access Code as parameter.
C#
protected void Logout(object sender, EventArgs e)
{
    FaceBookConnect.Logout(Request.QueryString["code"]);
}
 
VB.Net
Protected Sub Logout(sender As Object, e As EventArgs)
    FaceBookConnect.Logout(Request.QueryString("code"))
End Sub
 
1. Press the Login button in order to login through FaceBook
How to logout from FaceBook website using Graph API in ASP.Net
 
2. You will be asked to enter the login credentials at the FaceBook site
How to logout from FaceBook website using Graph API in ASP.Net
 
3. Now you need to provide permissions to the FaceBook App
How to logout from FaceBook website using Graph API in ASP.Net
 
4. Once permissions are provided, FaceBook App fetches user information
How to logout from FaceBook website using Graph API in ASP.Net
 
5. User has been logged out from FaceBook website through Graph API
How to logout from FaceBook website using Graph API in ASP.Net
 
 
Demo
 
Downloads