WP7 Facebook Logout Issues

Social media integration is a big part of any mobile app these days.  So I was adding Facebook and Twitter sharing to a WP7 app I’m working on.  This was my first experience using OAuth, so it didn’t go exactly smoothly but that’s for another post.  After some work I had Facebook/Twitter authentication and posting working great then the client asked for a logout feature so the user could change accounts if they like.

In my head, I thought no problem surely these APIs have a logout capability just like a login.  Not quite.  Apparently these networks like to keep you logged in once your in.  Twitter does allow you to re-authorize using another set of credentials but their API has no “logout” method.  Alternatively you can use the mobile logoff link in a WebBrowser to remove the session (cookies) and logout. Although this isn’t ideal, it works.

browser.Navigate(new Uri("http://mobile.twitter.com/session/destroy"));

On to Facebook, the API is similar, no direct logout option.  Again we would expect to be able to use the same workaround as twitter, just hit the logout url.  Nope, if you look at the source of the logout button on the Facebook page you will see its performing a POST with a few session keys.  So the link is different for each session.  The keys don’t match any of the OAuth keys I have so I can’t build the link either.

The next potential option is manually clearing the cookies in the WP7 browser.  Sorry, Microsoft doesn’t give us access to clear the cookies.  Some have attempted to work around this, for example

http://cloudstore.blogspot.com/2010/09/clearing-cookies-on-windows-phone-7-or.html

But as the WP7 API has changed these options are no longer working.

I had conceded at this point.  Facebook had won. We wouldn’t be able to allow users to logout.  Then I remembered one of the other logout alternatives suggested clearing the cookies from javascript.  I had tried this but it didn’t work.  But, if I can run JavaScript on a webpage, why can’t I use it to click the logout button on the Facebook page.  I was absolutely sure this wouldn’t work.  I tried anyways and it worked!  I couldn’t believe it.

LoadCompletedEventHandler loadCompleted = null;
loadCompleted = (sender, e) =>
{
    if (
        browser.SaveToString().Contains("logout_form"))
    {
        browser.InvokeScript("eval","document.forms['logout_form'].submit();");
        AppSettings.FacebookAccessKey= null;
        RaisePropertyChanged("LoggedIntoFacebook");
        browser.Visibility=Visibility.Collapsed;
        browser.LoadCompleted -= loadCompleted;
    }
};

browser.LoadCompleted += loadCompleted;
browser.Navigate(new Uri("https://www.facebook.com/logout.php"));

Happy coding!

7 thoughts on “WP7 Facebook Logout Issues

  1. Pingback: WP7 Facebook Logout Issues
  2. Pingback: WP7 Facebook Logout Issues – www.nalli.net
  3. Hi, the issue I’m seeing is that when I return to the login screen it automatically logs me in and returns “Success”. It seems somehow that session information (through cookies?) is kept even across new instances of WebBrowser.

Leave a Reply