Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Tagged as

Clearing Cookies on Windows Phone 7 or How to Log Out of Facebook

, 25 Oct 2010 CC (Attr 3U)
Rate this:
Please Sign up or sign in to vote.
Like many people it seems I’ve been working on an application for Windows Phone 7 that integrates with Facebook

Like many people it seems I’ve been working on an application for Windows Phone 7 that integrates with Facebook. Now that I’m getting close to completion I was looking at the use cases that fall outside of the core area such as logging out of Facebook to enable the user to use different credentials. Traditionally, you’d get the cookies for the relevant Facebook URLs and clear them out. Many people on the forums appear to have hit the same problem that I did in that there is no direct API in Windows Phone 7 to access cookies for the WebBrowser control. I managed to find a way round this and here’s how I did it:

Keeping Track of Cookies

There’s no direct API to get the cookies for the WebBrowser control, but you can get access to the cookies for a specific web request by providing a value for the CookieContainer property:

   1</span />:  private</span /> HttpWebRequest _webRequest;
   2</span />:  private</span /> CookieContainer _cookieContainer = new</span /> CookieContainer();
   3</span />:  ...
   4</span />:  this</span />._webRequest = (HttpWebRequest)HttpWebRequest.Create(uri);
   5</span />:  this</span />._webRequest.CookieContainer = this</span />._cookieContainer;

What you’ll notice here is that I’m creating an HttpWebRequest and an associated CookieContainer and passing that CookieContainer to the web request. What you’ll need to do is to keep hold of this web request for the lifetime of your application, or at the very least keep hold of the last web request that you used to access Facebook (depending on how busy your application is and what you’re doing with web requests. I managed to change my application code to use a single web request for the whole application lifetime). Then you can use the CookieContainer instance to find the relevant cookies.

Finding and Clearing Facebook’s Cookies

In order to track down Facebook’s cookies so that you can clear them out you need to ask the CookieContainer for the cookies that match a certain URL. I found that asking for https://login.facebook.com/login.php was all I needed to do, but your mileage may vary. I use this code in my WebClientHelper class (which holds my web request) to clear cookies for a specified URL:

   1</span />:  public</span /> void</span /> ClearCookies(Uri uri)
   2</span />:  {
   3</span />:      var</span /> cookies = this</span />._cookieContainer.GetCookies(uri);
   4</span />:      foreach</span /> (Cookie cookie in</span /> cookies)
   5</span />:      {
   6</span />:          cookie.Discard = true</span />;
   7</span />:          cookie.Expired = true</span />;
   8</span />:      }
   9</span />:  }

I found that setting both the Discard and Expired properties to true was sufficient to clear the cookies. It may well be that either one of them on their own is adequate; I haven’t had the opportunity to verify that.

So, the code that calls this method looks like this:

   1</span />:  private</span /> static</span /> readonly Uri FacebookCookieUri = new</span /> Uri("</span />https://login.facebook.com/login.php"</span />);
   2</span />:  ...
   3</span />:  this</span />._helper.ClearCookies(FacebookCookieUri);

If you then send the WebBrowser control to the login URL for Facebook as you would normally, then you should find that the cookies are cleared and the user is presented with the login dialog as usual.
I hope this helps.

License

This article, along with any associated source code and files, is licensed under The Creative Commons Attribution 3.0 Unported License

Share

About the Author

Derek Lakin
Software Developer (Senior)
United Kingdom United Kingdom
No Biography provided

Comments and Discussions

 
GeneralCan you give for me source code PinmemberThien La Duc13-Jun-11 22:03 

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
Web03 | 2.8.1411023.1 | Last Updated 25 Oct 2010
Article Copyright 2010 by Derek Lakin
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid