【问题标题】:Deleting cookies in WPF web browser control在 WPF Web 浏览器控件中删除 cookie
【发布时间】:2012-06-04 08:51:41
【问题描述】:

我正在开发一个使用 twitter API 的 WPF 应用程序。为了显示 twitter 身份验证页面,我正在使用 WPF Web 浏览器控件。我能够成功登录并使用 twitter API。我的问题是我需要清除 Web 浏览器的 cookie 才能实现注销功能。有什么方法可以清除 WPF Web 浏览器中的会话 cookie?

【问题讨论】:

    标签: wpf cookies webbrowser-control


    【解决方案1】:

    我没有对此进行测试,但我认为最好的方法是在页面上定义一个清除 cookie 的 Javascript 方法(如果可以的话)。

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

    (或任何 cookie 名称)。然后您可以在 WebBrowser 控件上使用InvokeScript 方法。

    【讨论】:

    • dbaseman,感谢您的回复,我正在使用 twitter API 访问 twitter,所以我认为我无法在该页面上定义 javascript 方法,还是我误解了什么?如果我是对的,那么有没有其他方法可以做到这一点?
    【解决方案2】:

    检查以下内容,

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

    【讨论】:

    • Ponmalar,感谢您的回复。我已经尝试过该链接,但这对我不起作用。我相信那里描述的方法会清除持久性 cookie 而不是会话 cookie。我需要清除会话 cookie。可能是澄清了问题。
    【解决方案3】:

    我昨天遇到了这个问题,今天终于想出了一个完整的解决方案。答案提到了here,更详细地介绍了herehere

    这里的主要问题是WebBrowser(在 WPF 和 WinForms 中)不允许您修改(删除)现有会话 cookie。这些会话 cookie 阻碍了多用户单设备体验的成功。

    上面链接中的 StackOverflow 响应省略了一个重要部分,它需要使用不安全的代码块,而不是使用 Marshal 服务。下面是一个完整的解决方案,可以放入您的项目中以抑制会话 cookie 持久性。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-22
      • 2010-11-19
      • 2011-02-18
      • 1970-01-01
      • 2012-03-01
      • 1970-01-01
      • 2011-01-20
      • 1970-01-01
      相关资源
      最近更新 更多