2009-01-12 73 views

回答

-1

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

所以解决方案是清除webBrowser控件的cookie。例如webBrowser1.Document.Cookies =“”,这应该工作,我认为。

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

1

webBrowser1.Document.Cookies =“”不起作用。此调用不会清除cookie。 webBrowser1.Document.Cookies =在javascript中就像document.cookie一样工作。 你应该找到你想要清除的cookie,sa'Session',使用 webBrowser1.Document.Cookies =“Session =''”; 只要将cookie设置为“',就可以。

4

如果您已启用JavaScript,您可以使用此代码段清除以清除网站浏览器当前所在网站的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控件的东西在长期运行。

45

要清除会话(如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); 
+3

这是正确的答案。 – EricLaw 2012-02-23 14:07:32

3

我什么都试过以清除表单数据,以便在未来的用户将不会看到以前的电子邮件地址,等我结束了这样做是为了清除饼干...

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

的Windows 7使用index.dat文件存储cookies和历史记录,以便Bill和他在中央情报局中心的朋友可以窥探你,并尽其所能确保你不能删除这些文件,并且在拷贝之后,因为使用了“特殊文件夹”并且.Dat文件在窗口运行时保持锁定状态。

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

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 { } 
    } 
0
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寻找这个答案。 快乐编码!