如何在不重新启动应用程序的情况下清除当前会话数据(cookie,缓存数据,auth会话等)?如何清除System.Windows.Forms.WebBrowser会话数据?
更新:我在谈论Windows.Forms中的WebBrowser控件,而不是ASP.Net会话。
如何在不重新启动应用程序的情况下清除当前会话数据(cookie,缓存数据,auth会话等)?如何清除System.Windows.Forms.WebBrowser会话数据?
更新:我在谈论Windows.Forms中的WebBrowser控件,而不是ASP.Net会话。
您必须认识到,从Web服务器的角度来看,“会话状态”被跟踪的方式是通过为客户端浏览器提供一个包含会话ID的cookie。当浏览器回发到服务器时,该cookie允许服务器将请求与存储的会话状态相关联。
所以解决方案是清除webBrowser控件的cookie。例如webBrowser1.Document.Cookies =“”,这应该工作,我认为。
ASP.NET也有它所谓的“无Cookie会话”,它通过将会话ID添加到URL来工作。因此,如果这是服务器使用的机制,则可以尝试将其过滤掉。但你不会看到太多,主要是基于cookie的会话状态。
webBrowser1.Document.Cookies =“”不起作用。此调用不会清除cookie。 webBrowser1.Document.Cookies =在javascript中就像document.cookie一样工作。 你应该找到你想要清除的cookie,sa'Session',使用 webBrowser1.Document.Cookies =“Session =''”; 只要将cookie设置为“',就可以。
如果您已启用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控件的东西在长期运行。
要清除会话(如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);
我什么都试过以清除表单数据,以便在未来的用户将不会看到以前的电子邮件地址,等我结束了这样做是为了清除饼干...
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)
{
}
}
的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 { }
}
以下解决方案为我工作 -
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
希望它有助于!!!
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寻找这个答案。 快乐编码!
这是正确的答案。 – EricLaw 2012-02-23 14:07:32