none
Deleting cookies in WPF web browser control RRS feed

  • Question

  • Hi All,

    I am developing a WPF application in which I am working with twitter API. To show twitter authentication page I am using WPF web-browser control. I am able to login and use twitter API successfully. My problem is that i need to clear web browser's cookies to implement logout functionality. Is there any way to clear session cookies in WPF web browser?

    I need to resolve this issue ASAP.

    Any help would be of great value.

    Thanks.


    Vibhore

    Wednesday, May 30, 2012 6:05 AM

Answers

  • Hi Vibhore,

    Check out this link: http://social.msdn.microsoft.com/Forums/en/wpf/thread/860d1b66-23c2-4a64-875b-1cac869a5e5d

    You may check out below code to delete cookies in WebBrowser from Matt for your reference:

    // For signout, we need to delete all cookies for these Urls.
            // (Based on empirical observation; there may be more later we need to clean to ensure logout)
            static readonly Uri FaceBookLoginUrl1 = new Uri("https://ssl.facebook.com/desktopapp.php");
            static readonly Uri FaceBookLoginUrl2 = new Uri("https://login.facebook.com/login.php");
            private static void _DeleteUserLoginCookie()
            {
                _DeleteEveryCookie(FaceBookLoginUrl1);
                _DeleteEveryCookie(FaceBookLoginUrl2);
            }
            private static void _DeleteEveryCookie(Uri url)
            {
                string cookie = string.Empty;
                try
                {
                    // Get every cookie (Expiration will not be in this response)
                    cookie = Application.GetCookie(url);
                }
                catch (Win32Exception)
                {
                    // "no more data is available" ... happens randomly so ignore it.
                }
                if (!string.IsNullOrEmpty(cookie))
                {
                    // This may change eventually, but seems quite consistent for Facebook.com.
                    // ... they split all values w/ ';' and put everything in foo=bar format.
                    string[] values = cookie.Split(';');
                    foreach (string s in values)
                    {
                        if (s.IndexOf('=') > 0)
                        {
                            // Sets value to null with expiration date of yesterday.
                            _DeleteSingleCookie(s.Substring(0, s.IndexOf('=')).Trim(), url);
                        }
                    }
                }
            }
            private static void _DeleteSingleCookie(string name, Uri url)
            {
                try
                {
                    // Calculate "one day ago"
                    DateTime expiration = DateTime.UtcNow - TimeSpan.FromDays(1);
                    // Format the cookie as seen on FB.com.  Path and domain name are important factors here.
                    string cookie = String.Format("{0}=; expires={1}; path=/; domain=.facebook.com", name, expiration.ToString("R"));
                    // Set a single value from this cookie (doesnt work if you try to do all at once, for some reason)
                    Application.SetCookie(url, cookie);
                }
                catch (Exception exc)
                {
                    Assert.Fail(exc + " seen deleting a cookie.  If this is reasonable, add it to the list.");
                }
            }

    And also here is a method of delete cookie for WindowsForm app WebBrowser using JavaScript for your reference:http://stackoverflow.com/questions/912741/c-webbrowser-control-how-to-delete-cookies-from-windows-form

    If it is worked, you can use WindowsFormHost to host a Winfrom Webbrowser in WPF app. Or try these JavaScript in WPF.

    Hope it helps.

    Have a nice day.


    Annabella Luo[MSFT]
    MSDN Community Support | Feedback to us

    Thursday, May 31, 2012 8:01 AM
    Moderator