【问题标题】:How to clear System.Windows.Forms.WebBrowser session data?如何清除 System.Windows.Forms.WebBrowser 会话数据?
【发布时间】:2010-09-30 20:56:40
【问题描述】:

如何在不重新启动应用程序的情况下清除当前会话数据(cookie、缓存数据、身份验证会话等)?

更新:我说的是 Windows.Forms 中的 WebBrowser 控件,而不是 ASP.Net 会话。

【问题讨论】:

    标签: c# .net browser webbrowser-control


    【解决方案1】:

    如果您启用了 javascript,您可以使用此代码 sn-p 清除以清除网络浏览器当前所在站点的 cookie(除此之外,我还没有找到清除会话 cookie 的方法)。

    webBrowser.Navigate("javascript:void((function(){var a,b,c,e,f;f=0;a=document.cookie.split('; ');for(e=0;e<a.length&&a[e];e++){f++;for(b='.'+location.host;b;b=b.replace(/^(?:%5C.|[^%5C.]+)/,'')){for(c=location.pathname;c;c=c.replace(/.$/,'')){document.cookie=(a[e]+'; domain='+b+'; path='+c+'; expires='+new Date((new Date()).getTime()-1e11).toGMTString());}}}})())")
    

    它来自this bookmarklet,用于清除cookie。

    除此之外,您可以删除“C:\Documents and Settings\username\Cookies”文件夹的内容(减去通常被锁定的 index.dat)。

    对于缓存的数据,只要删除“C:\Documents and Settings\username\Local Settings\Temporary Internet Files”中的所有文件就足够了。

    如果您真的需要能够清除所有网站的 cookie,从长远来看,您最好使用 axWebBrowser 控件之类的东西。

    【讨论】:

      【解决方案2】:
      Private Const INTERNET_OPTION_END_BROWSER_SESSION As Integer = 42
      
          <DllImport("wininet.dll", SetLastError:=True)>
          Public Shared Function InternetSetOption(hInternet As IntPtr, dwOption As Integer, lpBuffer As IntPtr, lpdwBufferLength As Integer) As Boolean
          End Function
      
          Private Sub WebBrowserFormName_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Closed
              InternetSetOption(IntPtr.Zero, INTERNET_OPTION_END_BROWSER_SESSION, IntPtr.Zero, 0)
          End Sub
      

      只是为在 VB 中寻找此答案的人发帖。 编码愉快!!!

      【讨论】:

        【解决方案3】:

        以下解决方案对我有用 -

        webBrowser1.Document.ExecCommand("ClearAuthenticationCache", false, null);
        

        这是在以下帖子中建议的删除 cookie - https://stackoverflow.com/a/21512662/6291511

        您可以在此处找到更多信息 - https://msdn.microsoft.com/en-us/library/system.windows.forms.htmldocument.execcommand(v=vs.110).aspx

        希望对你有帮助!!!

        【讨论】:

          【解决方案4】:

          Windows 7 使用 index.dat 文件来存储 cookie 和历史记录,以便比尔和他在 CIA 中心的朋友可以窥探您,并尽其所能确保您无法删除这些文件,并且在复制后因为“特殊使用文件夹,并且 .Dat 文件在 Windows 运行时保持锁定状态。

          这不是一个完美的解决方案,但它在某种程度上适用于列表中的完整文件名。

          int DeletedCount = 0;
                  int CouldNotDelete = 0;
                  KillExplorer();
                  foreach (string DatFile in DatFiles)
                  {//Do not put break point or step into the code else explorer will start and the file will become locked again
                      DirectoryInfo DInfo=new DirectoryInfo(DatFile.Replace("index.dat",""));
                      FileAttributes OldDirAttrib = DInfo.Attributes;
                      DInfo.Attributes  = FileAttributes.Normal;//Set to normal else can not delete
                      FileInfo FInfo = new FileInfo(DatFile);
                      FileAttributes OldFileAttrib = FInfo.Attributes;
                      SetAttr(FInfo, FileAttributes.Normal);
                      TryDelete(FInfo);
                      SetAttr(FInfo, OldFileAttrib);//Sets back to Hidden,system,directory,notcontentindexed
                      if (File.Exists(DatFile))
                          CouldNotDelete++;
                      else
                          DeletedCount++;
          
                  }
                  if (DatFiles.Count>0)//Lets get explorer running again
                      System.Diagnostics.Process.Start(DatFiles[DatFiles.Count - 1].Replace("index.dat", ""));
                  else
                      System.Diagnostics.Process.Start("explorer");
                  System.Windows.Forms.MessageBox.Show("Deleted " + DeletedCount + " Index.dat files with " + CouldNotDelete + " Errors");
          
          
                  return "Deleted " + DeleteFileCount + " Files ";
              }
          
              private void KillExplorer()
              {
                  foreach (Process P in Process.GetProcesses())
                  {//Kill both these process because these are the ones locking the files
                      if (P.ProcessName.ToLower() == "explorer")
                          P.Kill();
                      if (P.ProcessName.ToLower() == "iexplore")
                          P.Kill();
                  }
              }
          
              private bool TryDelete(FileInfo Info)
              {
                  try
                  {
                      Info.Delete();
                      return true;
                  }
                  catch 
                  {return false;}
              }
          
              private void SetAttr(FileInfo Info,FileAttributes Attr)
              {
                  try
                  {
                      Info.Attributes = Attr;
                  }
                  catch { }
              }
          

          【讨论】:

            【解决方案5】:

            我尽一切努力清除表单数据,以便下一个用户不会看到以前的电子邮件地址等。我最终这样做是为了清除 cookie...

            string[] theCookies = System.IO.Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.Cookies));
            foreach (string currentFile in theCookies)
            {
               try
               {
                  System.IO.File.Delete(currentFile);
               }
               catch (Exception ex)
               {
               }
            }
            

            【讨论】:

              【解决方案6】:

              要清除会话(例如 HttpOnly cookie),您可以使用 wininet.dll 中的 InternetSetOption()。

              private const int INTERNET_OPTION_END_BROWSER_SESSION = 42;
              
              [DllImport("wininet.dll", SetLastError = true)]
              private static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int lpdwBufferLength);
              

              并在需要清除会话时使用此方法。

              InternetSetOption(IntPtr.Zero, INTERNET_OPTION_END_BROWSER_SESSION, IntPtr.Zero, 0);
              webBrowser1.Document.Window.Navigate(url);
              

              【讨论】:

              • 我正在使用浏览器控件运行一组功能测试并且遇到了同样的问题。这个解决方案解决了我的问题。
              【解决方案7】:

              webBrowser1.Document.Cookies = "" 不起作用。此调用不会清除 cookie。 webBrowser1.Document.Cookies = 仅在 javascript 中用作 document.cookie。 您应该找到要清除的 cookie,例如“会话”,使用 webBrowser1.Document.Cookies = "会话 = ''"; 它只会根据需要将 cookie 设置为 ''。

              【讨论】:

                【解决方案8】:

                您必须意识到,从 Web 服务器的角度来看,跟踪“会话状态”的方式是向客户端浏览器提供一个带有会话 ID 的 cookie。当浏览器回传到服务器时,该 cookie 允许服务器将请求与存储的会话状态相关联。

                所以解决办法就是清除webBrowser控件的cookies。例如 webBrowser1.Document.Cookies = "",我认为应该可以。

                ASP.NET 也有所谓的“无cookie 会话”,它通过将会话ID 添加到url 来工作。因此,如果这是服务器使用的机制,您可以尝试将其从 url 中过滤掉。但是你不会看到那么多,它主要是基于 cookie 的会话状态。

                【讨论】:

                  猜你喜欢
                  • 2015-08-14
                  • 1970-01-01
                  • 1970-01-01
                  • 2016-10-29
                  • 2013-03-07
                  • 1970-01-01
                  • 2015-03-01
                  • 2013-12-06
                  • 2012-04-22
                  相关资源
                  最近更新 更多