Search code examples
asp.netfacebooklogoutlinkbutton

Logout of Facebook Through Linkbutton


I have been looking through quite a few posts on StackOverflow and on the internet on being able to log a user out of Facebook through an ASP.NET LinkButton.

I have tried implementing solutions from the following posts:

Code

ASPX Page

<asp:LinkButton ID="LogoutButton" CssClass="log-out fb"  OnClick="LogoutButton_Click" runat="server">Logout</asp:LinkButton>

JavaScript

$(".log-out.fb").click(function () {
    FB.logout(function (response) {
        //Logged out
        FB.Auth.setAuthResponse(null, 'unknown');
    });
});

HTML Output

<a id="MainContent_LogoutButton" class="log-out fb" href="javascript:__doPostBack('ctl00$MainContent$LogoutButton','')" style="width: 66px; ">Logout</a>

I definitely know that the jQuery click event is getting fired when debugging via Firebug. The jQuery code works fine when used in conjunction with a standard HTML anchor, so there is no reason for it not to work on a ASP.NET LinkButton.

Any help would be appreciated.


Solution

  • Thanks for all your help. But I managed to find a way to log out a user by using the following link:

    https://www.facebook.com/logout.php?next=YOUR_URL&access_token=ACCESS_TOKEN
    

    I created a callback page similar to the one from this article. Once I received the "access token", I managed to log the user out.

    Here is my code for my callback page:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!String.IsNullOrEmpty(Request["code"]) && !Page.IsPostBack)
        {
            FacebookCallback();
        }
    }
    
    private void FacebookCallback()
    {
        var client = new RestClient { Authority = "https://graph.facebook.com/oauth/" };
        var request = new RestRequest { Path = "access_token" };
    
        request.AddParameter("client_id", ConfigurationManager.AppSettings["facebook.appid"]);
        request.AddParameter("redirect_uri", ConfigurationManager.AppSettings["facebook.logout.callbackurl"]);
        request.AddParameter("client_secret", ConfigurationManager.AppSettings["facebook.appsecret"]);
        request.AddParameter("code", Request["code"]);
    
        RestResponse response = client.Request(request);
        // A little helper to parse the querystrings.
        StringDictionary result = QueryStringHelper.ParseQueryString(response.Content);
    
        string aToken = result["access_token"];
    
        LogUserOut(aToken);
    }
    
    private void LogUserOut(string sToken)
    {
        string url = String.Format("https://www.facebook.com/logout.php?next=http://{0}/Default.aspx&access_token={1}", ConfigurationManager.AppSettings["site.url"], sToken);
    
        Response.Redirect(url);
    }
    

    I hope this helps others if they encounter the same issue.