Click here to Skip to main content
11,703,438 members (71,856 online)
Click here to Skip to main content

Facebook SDK for .NET - How to log your application out

, 3 Jul 2013 CPOL 17.9K 11
Rate this:
Please Sign up or sign in to vote.
How to log your application out 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:

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:

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

  • Version 1.00 - Initial publish.

License

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

Share

About the Author

Johnny J.
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 .NET developer building Airline Booking Systems, and have a number of projects in various states of progress to work on in the spare time...

PS: The cat on my profile is one of my three cats, Ramses. He's all white, odd-eyed, deaf and definitely the coolest cat there is!

You may also be interested in...

Comments and Discussions

 
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    Rant Rant    Admin Admin   

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

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150819.1 | Last Updated 3 Jul 2013
Article Copyright 2013 by Johnny J.
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid