2009-01-12 74 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尋找這個答案。 快樂編碼!