Xamarin.Auth,WebView在Android上清除Cookie

eth*_*ane 3 cookies android-webview xamarin.auth xamarin.forms

关于使用Xamarin.Auth清除Android WebView的cookie,我已经尝试了所有可以在线找到的内容。Auth库不会公开Android WebView。我无法使用其WebSettings,也无法清除该WebView对象上的缓存。

Xamarin.Auth公开了一种清除cookie的方法:

 public static void ClearCookies()
 {
        global::Android.Webkit.CookieSyncManager.CreateInstance(global::Android.App.Application.Context);
        global::Android.Webkit.CookieManager.Instance.RemoveAllCookie();
 }
Run Code Online (Sandbox Code Playgroud)

这似乎对Cookie没有影响。通过Chrome进行调试时,我可以看到Cookie,并在其中清除它确实会删除所有Cookie。

我已经尝试过CookieManager.Instance.RemoveAllCookies(null);CookieManager.Instance.RemoveSessionCookies(null);,在Xamarin.Auth创建自己的实例之前创建一个新的WebView,将SetAcceptCookies设置为false,清除WebViewStorage,然后删除“ webview.db”和“ webviewCache.db”。但所有cookie仍然保留。

我看了荒谬的建议和答案。

使用Xamarin.Auth v1.5.0.3并在S4 Mini,S7和LG G3 Beat上进行测试。

*编辑
由于CookieManager.Instance.Sync()是异步运行的,是否可能未及时完成或根本就没有运行?

Ven*_*aju 5

下面的代码对您有用

Xamarin.Android:

 var cookieManager = CookieManager.Instance;
 cookieManager.RemoveAllCookie();
Run Code Online (Sandbox Code Playgroud)

Xamarin.iOS:

 NSHttpCookieStorage CookieStorage = NSHttpCookieStorage.SharedStorage;
        foreach (var cookie in CookieStorage.Cookies)
            CookieStorage.DeleteCookie(cookie);
      }
Run Code Online (Sandbox Code Playgroud)

Xamarin.Forms:

PCL:

IClearCookies.cs

 using System;
 namespace POCDemo
 {
    public interface IClearCookies
     {
        void Clear();
     }
 }
Run Code Online (Sandbox Code Playgroud)

Android:

IClearCookiesImplementation.cs

using POCDemo.Droid;
using Xamarin.Forms;
using System.Net;
using Android.Webkit;

[assembly: Dependency(typeof(IClearCookiesImplementation))]
namespace POCDemo.Droid{
public class IClearCookiesImplementation : IClearCookies{
    public void Clear(){
        var cookieManager = CookieManager.Instance;
        cookieManager.RemoveAllCookie();
      }
    }
 }
Run Code Online (Sandbox Code Playgroud)

的iOS

IClearCookiesImplementation.cs

using POCDemo.iOS;
using Xamarin.Forms;
using System.Net;
using Foundation;

[assembly: Dependency(typeof(IClearCookiesImplementation))]
namespace POCDemo.iOS{
public class IClearCookiesImplementation : IClearCookies{
    public void Clear(){
        NSHttpCookieStorage CookieStorage = NSHttpCookieStorage.SharedStorage;
        foreach (var cookie in CookieStorage.Cookies)
            CookieStorage.DeleteCookie(cookie);
      }
   }
}
Run Code Online (Sandbox Code Playgroud)

呼叫依存服务

PCL:

DependencyService.Get<IClearCookies>().Clear();
Run Code Online (Sandbox Code Playgroud)

对我有用


Mel*_*son 2

我使用这些代码行取得了成功:

CookieManager.Instance.RemoveAllCookie();
CookieManager.Instance.RemoveSessionCookie();
CookieManager.Instance.Flush();
CookieSyncManager.Instance.Sync();
Run Code Online (Sandbox Code Playgroud)