How do I clear cookies in WPF WebBrowser for a specific site?

How do I delete cookies for a specific website or page? Currently, if I login using OAuth 2.0 via WPF WebBrowser, my login session persists, but I want to reset the session every time I close my app.

public partial class VKLogin : Window
    {
        public string AccessToken { get; set; }

        public VKLogin()
        {
            InitializeComponent();

            this.Loaded += (object sender, RoutedEventArgs e) =>
            {
                webBrowser.Navigate("https://oauth.vk.com/authorize?client_id=5965945&scope=wall&redirect_uri=https://oauth.vk.com/blank.html&display=page&v=5.63&response_type=token");
            };
        }

        private void webBrowser_Navigated(object sender, NavigationEventArgs e)
        {
            var url = e.Uri.Fragment;
            if (url.Contains("access_token") && url.Contains("#"))
            {
                url = (new System.Text.RegularExpressions.Regex("#")).Replace(url, "?", 1);
                AccessToken = System.Web.HttpUtility.ParseQueryString(url).Get("access_token");
            }
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            webBrowser.Dispose();
        }
    }

      

XAML

<Grid>
    <WebBrowser Name="webBrowser" 
            Navigated="webBrowser_Navigated" 
             />
</Grid>

      

+3


source to share


1 answer


If you want to delete all cookies, you can use the InternetSetOption function , explained here .

The code will look like this:

[System.Runtime.InteropServices.DllImport("wininet.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto, SetLastError = true)]
public static extern bool InternetSetOption(int hInternet, int dwOption, IntPtr lpBuffer, int dwBufferLength);

private static unsafe void SuppressWininetBehavior()
{
    /* SOURCE: http://msdn.microsoft.com/en-us/library/windows/desktop/aa385328%28v=vs.85%29.aspx
        * INTERNET_OPTION_SUPPRESS_BEHAVIOR (81):
        *      A general purpose option that is used to suppress behaviors on a process-wide basis. 
        *      The lpBuffer parameter of the function must be a pointer to a DWORD containing the specific behavior to suppress. 
        *      This option cannot be queried with InternetQueryOption. 
        *      
        * INTERNET_SUPPRESS_COOKIE_PERSIST (3):
        *      Suppresses the persistence of cookies, even if the server has specified them as persistent.
        *      Version:  Requires Internet Explorer 8.0 or later.
        */

    int option = (int)3/* INTERNET_SUPPRESS_COOKIE_PERSIST*/;
    int* optionPtr = &option;

    bool success = InternetSetOption(0, 81/*INTERNET_OPTION_SUPPRESS_BEHAVIOR*/, new IntPtr(optionPtr), sizeof(int));
    if (!success)
    {
        //Something went wrong
    }
}

      

and then use the function after InitializeComponent();

like:



SuppressWininetBehavior();

      

Alternatively, if you have Javascript enabled, you can use the following code:

function delete_cookie( name ) {
  document.cookie = name + '=; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
}

      

+3


source







All Articles