从WebView清除所有cookie [英] Clear all cookies from WebView
问题描述
我目前正在使用UWP开发适用于Windows Phone 10的应用程序,其中需要一个WebView,该程序将在此过程中设置多个cookie。 WebView可以被实例化很多次,并且我注意到cookie不会在多个实例中清除,这使我的工作流程有点困难(例如,用户必须使用WebView登录,然后重新实例化,就会有旧状态。)
I am currently developing an application for Windows Phone 10 using UWP, in which I need a WebView that will set up several cookies in the process. The WebView can be instantiated many times, and I have noticed that the cookies do not get cleared on multiple instantiations, which makes it a bit difficult for my workflow (say, for instance, a user has to log in using the WebView, then when re-instantiating, the old state will be there).
我尝试使用 clearTemporaryWebDataAsync
现在可用。更糟糕的是,我事先不知道与Cookie关联的域,因此我无法执行以下操作:
I have tried using the clearTemporaryWebDataAsync
to now avail. What is worse is that I don't know in advance the domains associated to the cookies, so I cannot do something like this:
HttpBaseProtocolFilter myFilter = new HttpBaseProtocolFilter();
HttpCookieManager cookieManager = myFilter.CookieManager;
HttpCookieCollection myCookieJar = cookieManager.GetCookies(new Uri("https://url.com"));
foreach (HttpCookie cookie in myCookieJar)
{
cookieManager.DeleteCookie(cookie);
}
API中是否有任何内容可以帮助我清除所有Cookie WebView,或者至少获取它们的列表,以便我可以在每个上使用 DeleteCookie
?
Is there anything in the API that might help me wipe out all the cookies for the WebView, or at least get a list of them so that then I can use DeleteCookie
on each?
推荐答案
更糟糕的是,我事先不知道与cookie关联的域,所以我不能做这样的事情:
What is worse is that I don't know in advance the domains associated to the cookies, so I cannot do something like this:
AFAIK,除 ClearTemporaryWebDataAsync
。但是您提供的代码段可以清除cookie。对于您认为不知道事先了解Cookie的域的信息,可以通过 NavigationStarting
事件句柄。例如,
AFAIK, there is no more method for clearing cache data except ClearTemporaryWebDataAsync
. But the code snippet you provided do clear cookies. For what you consider about don't know in advance the domains to the cookies, you may get the current navigating Uri by the WebViewNavigationStartingEventArgs
of NavigationStarting
event handle. For example,
private void WebViewControl2_NavigationStarting(WebView sender, WebViewNavigationStartingEventArgs args)
{
Uri gotouri = args.Uri;
HttpBaseProtocolFilter myFilter = new HttpBaseProtocolFilter();
HttpCookieManager cookieManager = myFilter.CookieManager;
HttpCookieCollection myCookieJar = cookieManager.GetCookies(gotouri);
foreach (HttpCookie cookie in myCookieJar)
{
cookieManager.DeleteCookie(cookie);
}
}
这将清除当前Uri的cookie。但这会在每次 WebView
导航时清除cookie。您可能需要设置一个标志,以确保 WebView
是否为新实例,具体取决于您的应用逻辑。
And this will clear the cookies of current Uri. But this will clear the cookies every time the WebView
naviagation. You may need to set a flag to ensure if the WebView
is new instance depending on your app logic.
另一个东西,在为Windows 10编译的应用中, WebView 使用Microsoft Edge渲染引擎显示HTML内容。清除当前 WebView
实例的cookie也可能会清除其他实例,这与您发现拥有新实例时cookie仍然存在一样。
Another thing, in apps compiled for Windows 10, WebView uses the Microsoft Edge rendering engine to display HTML content. Clear cookies of current WebView
instance may also clear others, which is same as you found cookies still exists when you have a new instance.
这篇关于从WebView清除所有cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!