Logging Out of Facebook with C# SDK


Posted by Cynic | Posted in .NET, C#, Software, Solutions to Problems, Super Simple | Posted on 22-06-2011


FacebookLogging out of Facebook programmatically turns out to be much more problematic that you would think.

I posted the question at StackOverflow asking if anyone knew of a way to get it done. The only answer, other than mine, turned up the same results I’d been having, i.e. You just can’t logout easily.

So, I was forced to resort to nasty, dirty, hacky, ugly measures to get the job done. Using the FacebookOAuthClient.GetLogoutUrl() method didn’t work. I tried and tried and tried. I manually constructed URLs in many different combinations and permutations, and nothing worked.

The problem, as it turns out, isn’t limited to C# though, as many other developers using different SDKs and languages have had similar problems.

The general answer seems to be to use the Facebook JavaScript SDK. Huh? Well, when you’re not developing a web page, it’s kind of tough to use that SDK, and any attempt would be pretty hacky and ugly and time consuming. And then again, there’s no guarantee that it would work either.

As such, I resorted to browser automation… Ick. Yucky. It’s prone to breakage and just all-round icky. So, here’s the basics on how I am logging out of Facebook…

private void wbrFacebookAuth_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
   if (_logout)
    HtmlElementCollection hec = wbrFacebookAuth.Document.GetElementsByTagName("a");
    foreach (HtmlElement elem in hec)
        // the logout link has a data-sigil="logout" attribute:
        string datasigil = elem.GetAttribute("data-sigil").ToLower();
        if (datasigil == "logout")

The code finds all the links in the page, then iterates through them to find the logout link, and then navigates to it. It works. But that’s all I can say about it.

It’s really rather unfortunate as the Facebook API documentation is simply some of the poorest I’ve ever seen. There are many comments from developers in every section of the documentation asking for better docs as the documentation available right now doesn’t answer much of anything.

They don’t have any low-level documentation that would allow you to simply look up an issue, then create a solution for it. If there were, I would have been able to quickly write something that wasn’t a horrendous hack like the abomination above.

I suppose that in some ways I’ve simply got more of a desktop/server mentality now, and just don’t have that hoodoo magic needed to divine WTF comes next in the web world.

Maybe I should head out to see if I can buy some chickens to sacrifice… Maybe that will help…

But for now, the Facebook functionality in the Super Simple Photo Resizer is working.



  • Ky

    sometimes logout does not simply mean “log out”. =D