アプリケーションを再起動せずに現在のセッション データ (Cookie、キャッシュ データ、認証セッションなど) をクリアするにはどうすればよいですか?
更新: ASP.Net セッションではなく、Windows.Forms の WebBrowser コントロールについて話しています。
アプリケーションを再起動せずに現在のセッション データ (Cookie、キャッシュ データ、認証セッションなど) をクリアするにはどうすればよいですか?
更新: ASP.Net セッションではなく、Windows.Forms の WebBrowser コントロールについて話しています。
セッション(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);
次のユーザーに以前のメールアドレスなどが表示されないように、フォームデータをクリアするためにあらゆることを試みました.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)
{
}
}
次の解決策は私のために働いた -
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 を参照してください。
それが役に立てば幸い!!!
webBrowser1.Document.Cookies = "" は機能しません。この呼び出しは Cookie をクリアしません。webBrowser1.Document.Cookies = JavaScript の document.cookie として機能します。クリアしたい Cookie を見つける必要があります。sa 'Session', use webBrowser1.Document.Cookies = "Session = ''"; 必要に応じて、Cookie を '' に設定するだけです。
Web サーバーの観点から「セッション状態」を追跡する方法は、セッション ID を含む Cookie をクライアント ブラウザーに与えることであることに注意してください。ブラウザーがサーバーにポストバックすると、その Cookie により、サーバーは要求を保存されたセッション状態に関連付けることができます。
したがって、解決策は、webBrowser コントロールの Cookie をクリアすることです。たとえば、webBrowser1.Document.Cookies = ""、それはうまくいくはずです。
ASP.NET には、"cookieless セッション" と呼ばれるものもあり、セッション ID を URL に追加することで機能します。したがって、それがサーバーで使用されるメカニズムである場合は、URL からそれを除外することを試みることができます。しかし、それほど多くは表示されません。ほとんどの場合、Cookie ベースのセッション状態です。
Windows 7 は index.dat ファイルを使用して Cookie と履歴を保存するため、Bill と彼の友人である CIA 中央があなたを詮索し、これらのファイルを削除できないようにできる限りのことを行っています。使用され、Windows の実行中は .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 { }
}