我正在開發一個WPF應用程序,我正在使用twitter API。要顯示Twitter認證頁面,我正在使用WPF Web瀏覽器控件。我能夠成功登錄並使用twitter API。我的問題是,我需要清除Web瀏覽器的Cookie來實現註銷功能。有沒有什麼方法可以清除WPF Web瀏覽器中的會話cookie?刪除WPF Web瀏覽器控件中的cookie
2
A
回答
3
檢查以下,
http://social.msdn.microsoft.com/Forums/en/wpf/thread/860d1b66-23c2-4a64-875b-1cac869a5e5d
private static void _DeleteSingleCookie(string name, Uri url)
{
try
{
// Calculate "one day ago"
DateTime expiration = DateTime.UtcNow - TimeSpan.FromDays(1);
// Format the cookie as seen on FB.com. Path and domain name are important factors here.
string cookie = String.Format("{0}=; expires={1}; path=/; domain=.facebook.com", name, expiration.ToString("R"));
// Set a single value from this cookie (doesnt work if you try to do all at once, for some reason)
Application.SetCookie(url, cookie);
}
catch (Exception exc)
{
Assert.Fail(exc + " seen deleting a cookie. If this is reasonable, add it to the list.");
}
}
0
我還沒有測試過這個,但我認爲最好的方法是在頁面上定義一個Javascript方法(如果可以的話)來清除cookie。
document.cookie='c_user=;expires=Thu, 01 Jan 1970 00:00:00 GMT;domain=.facebook.com';
(或任何cookie名稱)。然後您可以使用InvokeScript方法對WebBrowser進行控制。
+0
dbaseman,感謝您的回覆,我使用Twitter的API來訪問Twitter,所以我不認爲我可以在頁面上定義一個JavaScript方法,還是我誤解的東西嗎?如果我是對的,那麼還有其他方法可以做到嗎? –
3
我昨天就遇到了這個問題,今天終於想出了一個完整的解決方案。答案提到here這是更詳細的here和here。
這裏的主要問題是WebBrowser
(在WPF和WinForms中)不允許您修改(刪除)現有會話cookie。這些會話cookie是阻止多用戶單一設備體驗成功的原因。
以上鍊接中的StackOverflow響應省略了重要部分,它需要使用不安全的代碼塊,而不是使用Marshal
服務。下面是一個完整的解決方案,可以放置到您的項目中,以抑制會話cookie持久性。
public static partial class NativeMethods
{
[DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int dwBufferLength);
private const int INTERNET_OPTION_SUPPRESS_BEHAVIOR = 81;
private const int INTERNET_SUPPRESS_COOKIE_PERSIST = 3;
public static void SuppressCookiePersistence()
{
var lpBuffer = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(int)));
Marshal.StructureToPtr(INTERNET_SUPPRESS_COOKIE_PERSIST, lpBuffer, true);
InternetSetOption(IntPtr.Zero, INTERNET_OPTION_SUPPRESS_BEHAVIOR, lpBuffer, sizeof(int));
Marshal.FreeCoTaskMem(lpBuffer);
}
}
相關問題
- 1. 停止從WPF web瀏覽器控件
- 2. Web瀏覽器控件在WPF
- 3. 從瀏覽器中刪除cookie?
- 4. 從瀏覽器中刪除HTTP_COOKIE cookie
- 5. 從Eclipse的內部Web瀏覽器中刪除cookie和緩存
- 6. 從Web瀏覽器控件中刪除滾動條
- 7. C#Web瀏覽器控件
- 8. 的WinForms web瀏覽器:如何刪除Cookie
- 9. 刪除Java中的cookie /瀏覽器如何刪除cookies
- 10. 允許/阻止WinForms Web瀏覽器控件中的Cookie
- 11. 刪除瀏覽器Cookie(跨域)
- 12. 關閉瀏覽器時cookie被刪除
- 13. 瀏覽器何時刪除會話cookie?
- 14. 如何在Web瀏覽器控件中處理Cookie?
- 15. web瀏覽器控件
- 16. VB.NET Web瀏覽器控件
- 17. Web瀏覽器控件
- 18. Web瀏覽器控件
- 19. 刪除了文件瀏覽器中的文件刪除器
- 20. 將項目資源加載到WPF Web瀏覽器控件中
- 21. Windows窗體Web瀏覽器用戶控件在WPF中更新
- 22. WPF Web瀏覽器替代
- 23. c#web瀏覽器Cookie中httpwebrequest
- 24. 如何刪除在瀏覽器控件中導航的文件?
- 25. 使用自定義滾動條的WPF Web瀏覽器控件
- 26. WPF - 如何顯示Web瀏覽器控件的加載消息
- 27. C#/ WPF:與Web瀏覽器控件的交互?
- 28. 在wpf網絡瀏覽器控件中瀏覽安全網頁
- 29. Web瀏覽器控件的進度條
- 30. 點擊事件沒有刪除瀏覽器cookie
Ponmalar,謝謝你的回覆。我已經嘗試了這個鏈接,但是這對我沒有幫助。我相信那裏描述的方法會清除持久cookie而不是會話cookie。我需要清除會話cookie。可能會說明問題。 –
你檢查過http://stackoverflow.com/questions/912741/c-webbrowser-control-how-to-delete-cookies-from-windows-form – Ponmalar