【问题标题】:How can I clear cookies in the WPF WebBrowser for a specific site?如何清除 WPF WebBrowser 中特定站点的 cookie?
【发布时间】:2017-08-31 02:40:05
【问题描述】:

如何删除特定网站或其页面的身份验证 cookie?目前,如果我通过 WPF WebBrowser 使用 OAuth 2.0 登录,我的登录会话将被保存,但我想在每次关闭应用程序时重置会话。

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>

【问题讨论】:

    标签: c# wpf cookies webbrowser-control


    【解决方案1】:

    如果你想删除所有cookies,你可以使用InternetSetOption function,解释here

    代码如下:

    [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
        }
    }
    

    然后在InitializeComponent();之后使用调用函数如:

    SuppressWininetBehavior();
    

    另外,如果您启用了 Javascript,您可以使用以下代码:

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

    【讨论】:

      猜你喜欢
      • 2013-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-14
      • 2011-01-03
      • 1970-01-01
      相关资源
      最近更新 更多