Click here to Skip to main content
15,884,176 members
Articles / Programming Languages / Javascript
Tip/Trick

Facebook SDK for .NET - How to Log Your Application Out

Rate me:
Please Sign up or sign in to vote.
4.33/5 (4 votes)
3 Jul 2013CPOL3 min read 30.8K   11   2
How to log your application out from Facebook using the Facebook SDK for .NET
In this tip, you will learn how to log out your application from Facebook using the Facebook SDK for .NET.

Background

I'm doing a small Windows application for fun that uses the Facebook SDK for .NET (NuGet version 6.4.2) to log into Facebook and retrieve some information.

The thought is that the user should be able to log out again and log in with another account if he/she so wishes.

I use a WebBrowser control to present the user with the login dialog, and once the login is done and I have received the access token, I hide that web browser and do my nasty business.

That part was actually quite easy to do. I've posted a full article about the whole Facebook interaction thingy elsewhere, so I'm not going to go into that now.

The full article can be found here: FriendTracker - A Winforms application that interacts with Facebook

I thought that implementing the logout functionality would be just as simple. According to the documentation, this code should do it:

C#
private string DoLogOut()
{
    dynamic parameters = new ExpandoObject();

    parameters.next = HttpUtility.UrlEncode(@"http://www.google.com", Encoding.UTF8);
    parameters.access_token = _accessToken;

    var fb = new FacebookClient(_accessToken);

    Uri logoutUri = fb.GetLogoutUrl(parameters);
    
    loginWebBrowser.Navigate(logoutUri.AbsoluteUri);
} 

Navigating to the Logout url should log the user out of Facebook and redirect the webbrowser to the URL specified in the "next" parameter.

I've found varying information about whether or not it's necessary to UrlEncode the "next" URL and whether or not it's necessary to specify the access token as a specific parameter and not just in the constructor of the FacebookClient object. So I tried a lot of different combinations.

But no matter what I did, it wouldn't work. All that happened was that my webbrowser was redirected to the Facebook timeline, and that the user was STILL logged in.

I Googled for many hours, and I saw that a lot of other developers had the same problem - Some claim that it's a known Facebook bug, but seeing that it seems to have been around for at least 2-3 years, I find it hard to believe that they haven't had time to fix it yet; Nevertheless, I found a lot of different suggestions to solutions and none of them seemed to work.

One suggestion for a dirty workaround (but the only solution I can find right now that does the job) is to inject a JavaScript in the Facebook timeline page and programmatically invoke it to click the logout button.

That solution isn't applicable straight off the shelf, though, because it seems like there is no longer any logout button on the timeline page. There IS, however, a menu with a "Log out" menu item that submits a hidden logout form.

So my solution to everything is to check if the page contains a form with the id "logout_form", and if it does, I inject a bit of JavaScript to submit the form.

I do that in the WebBrowser Navigated event:

C#
private void LoginWebBrowserNavigated(object sender, WebBrowserNavigatedEventArgs e)
{
    if (_loggingOut)
    {
        if (loginWebBrowser.Document != null)
        {
            HtmlElement logoutForm = loginWebBrowser.Document.GetElementById("logout_form");
            if (logoutForm != null)
            {
                loginWebBrowser.Document.InvokeScript("execScript", 
                  new Object[] { "document.getElementById('logout_form').submit();", 
                                 "JavaScript" });
            }
        }
        if (e.Url.AbsoluteUri.StartsWith(@"https://www.facebook.com/index.php") || 
            e.Url.AbsoluteUri.StartsWith(@"http://www.facebook.com/index.php"))
        {
            loginWebBrowser.Navigate(_loginUrl);
        }
    }
}

The last piece of code that checks for index.php is used to redirect back to the login page so that the user can log in with another account (or the same once more).

Don't be confused by the name loginWebBrowser - It's the same webbrowser used for the login and logout. I could have named it differently, but at the end of the day, I decided that it didn't matter a whole lot.

History

  • 2nd July, 2013: Version 1.00 - Initial publication

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Sweden Sweden
Born in Copenhagen, Denmark
Have been living in Paris, France and L.A., The United States
Now live in Stockholm, Sweden

Started programming when I got my first VIC 20, and a few months later on Commodore 64. Those were the days!

Studied programming at the Copenhagen Engineering Academy

Professional console, winforms and webforms programming in Comal, x86 Assembler, Fortran, Pascal, Delphi, Visual Basic 3 through 6, Classic ASP, C# and VB.NET

I now work as Senior Microsoft Dynamics AX and .Net programmer, and have a number of projects in various states of progress to work on in the spare time...

Comments and Discussions

 
QuestionTo get it to work you must also inform the Access Token Pin
TonyBR10-Apr-17 8:15
TonyBR10-Apr-17 8:15 
GeneralMy vote of 5 Pin
Aatif Ali from Bangalore4-Jul-13 2:33
professionalAatif Ali from Bangalore4-Jul-13 2:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.