2009-01-12 27 views

Trả lời

-1

Bạn phải nhận ra rằng cách "trạng thái phiên" được theo dõi, từ quan điểm của máy chủ web, bằng cách cung cấp cho trình duyệt của khách một cookie có id phiên trong đó. Khi trình duyệt đăng lại máy chủ, cookie đó cho phép máy chủ liên kết yêu cầu với trạng thái phiên được lưu trữ.

Vì vậy, giải pháp là xóa cookie của điều khiển webBrowser. Ví dụ: webBrowser1.Document.Cookies = "", điều đó sẽ làm việc tôi nghĩ.

ASP.NET cũng có những gì nó gọi là "phiên vô dụng", hoạt động bằng cách thêm id phiên vào url. Vì vậy, nếu đó là cơ chế được máy chủ sử dụng, bạn có thể thử lọc ra khỏi url. Nhưng bạn sẽ không thấy nhiều, phần lớn là trạng thái phiên dựa trên cookie.

1

webBrowser1.Document.Cookies = "" sẽ không hoạt động. Cuộc gọi này sẽ không xóa cookie. webBrowser1.Document.Cookies = chỉ hoạt động như document.cookie trong javascript. Bạn nên tìm cookie mà bạn muốn xóa, hãy dùng '01', sử dụng webBrowser1.Document.Cookies = "Session = ''"; Nó sẽ chỉ đặt cookie thành '', như bạn muốn.

4

Nếu bạn đã bật javascript, bạn chỉ có thể sử dụng đoạn mã này để xóa cookie cho trang web mà trình duyệt web hiện đang bật (tôi chưa tìm thấy cách xóa cookie phiên khác với điều này).

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

Nó có nguồn gốc từ this bookmarklet để xóa cookie.

Ngoài ra, bạn có thể xóa nội dung của thư mục `` C: \ Documents and Settings \ username \ Cookies '' (trừ chỉ mục index.dat, thường bị khóa).

Đối với dữ liệu được lưu trong bộ nhớ cache, bạn chỉ cần xóa tất cả các tệp trong `C: \ Documents and Settings \ username \ Local Settings \ Temporary Internet Files ''.

Nếu bạn thực sự cần phải có thể xóa cookie cho tất cả các trang web, có lẽ bạn nên sử dụng một cái gì đó giống như điều khiển axWebBrowser trong thời gian dài.

45

Để xóa phiên (chẳng hạn như cookie HttpOnly), bạn có thể sử dụng InternetSetOption() từ wininet.dll.

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

và sử dụng phương pháp này bất cứ khi nào cần xóa phiên.

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

Đây là câu trả lời đúng. – EricLaw

+1

Tôi đã chạy một tập hợp các bài kiểm tra chức năng bằng cách sử dụng điều khiển trình duyệt và đã có vấn đề này rất giống nhau. Giải pháp này đã giải quyết được vấn đề của tôi. – Zoidberg

3

tôi đã cố gắng mọi thứ để xóa các dữ liệu mẫu để người dùng tiếp theo sẽ không thấy địa chỉ email trước, vv Tôi đã kết thúc làm điều này để xóa các cookies ...

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 sử dụng các tệp index.dat để lưu trữ cookie và lịch sử để Bill và freinds của mình tại trung tâm CIA có thể tìm thấy bạn và đã làm tất cả những gì họ có thể để đảm bảo bạn không thể xóa các tệp này và sau khi lấy bản sao vì 'Thư mục đặc biệt' được sử dụng và các tệp .Dat vẫn bị khóa trong khi các cửa sổ đang chạy.

Đây không phải là giải pháp hoàn hảo nhưng nó hoạt động ở một mức độ nào đó với tên tệp đầy đủ nằm trong Danh sách.

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 

Chỉ cần đăng bài cho người đang tìm câu trả lời này trong VB. Chúc mừng mã hóa !!!

Các vấn đề liên quan