Deleting cookies in WPF web browser control

两盒软妹~` 提交于 2019-12-22 10:45:11

问题


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?


回答1:


Check the following,

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

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.");
        }
    }



回答2:


I ran into this issue yesterday and finally came up with a full solution today. The answer is mentioned here which is put into more detail here and here.

The primary issue here is that the WebBrowser (in WPF and WinForms) does not permit you to modify (delete) existing session cookies. These session cookies are what prevent a multi-user single device experience from being successful.

The StackOverflow response in the link above omits an important part, it requires the use of an unsafe code block, instead of using the Marshal service. Below is a full solution that can be placed into your project to suppress the session cookie persistence.

public static partial class NativeMethods
{
    [DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int dwBufferLength);

    private const int INTERNET_OPTION_SUPPRESS_BEHAVIOR = 81;
    private const int INTERNET_SUPPRESS_COOKIE_PERSIST = 3;

    public static void SuppressCookiePersistence()
    {
        var lpBuffer = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(int)));
        Marshal.StructureToPtr(INTERNET_SUPPRESS_COOKIE_PERSIST, lpBuffer, true);

        InternetSetOption(IntPtr.Zero, INTERNET_OPTION_SUPPRESS_BEHAVIOR, lpBuffer, sizeof(int));

        Marshal.FreeCoTaskMem(lpBuffer);
    }
}



回答3:


I have not tested this, but I think the best way would be to define a Javascript method on the page (if you're able to) that clears the cookie.

document.cookie='c_user=;expires=Thu, 01 Jan 1970 00:00:00 GMT;domain=.facebook.com';

(or whatever the cookie name is). Then you can use the InvokeScript method on the WebBrowser control.



来源:https://stackoverflow.com/questions/10810881/deleting-cookies-in-wpf-web-browser-control

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!