7

I've tried probably everything I could find online regarding clearing the cookies for an Android WebView using Xamarin.Auth. The Auth library does not expose the Android WebView; I cannot use its WebSettings nor clear the cache on that WebView object.

Xamarin.Auth exposes a method for clearing cookies:

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

which does not seem to have an effect on cookies. I can see the cookies while debugging through Chrome and clearing it there does remove all cookies.

I have tried CookieManager.Instance.RemoveAllCookies(null); and CookieManager.Instance.RemoveSessionCookies(null);, creating a new WebView before Xamarin.Auth creates its own instance, setting SetAcceptCookies to false, clearing WebViewStorage, and deleting "webview.db" and "webviewCache.db." but all cookies still remain.

I've looked an absurd amount of suggestions and answers.

Using Xamarin.Auth v1.5.0.3 and testing on S4 Mini, S7, LG G3 Beat.

*Edit
Since CookieManager.Instance.Sync() runs asyncronously, could it be that this isn't completing in time or simply doesn't run?

ethane
  • 2,329
  • 3
  • 22
  • 33

2 Answers2

10

Below code useful for you

Xamarin.Android:

 var cookieManager = CookieManager.Instance;
 cookieManager.RemoveAllCookie();

Xamarin.iOS:

 NSHttpCookieStorage CookieStorage = NSHttpCookieStorage.SharedStorage;
        foreach (var cookie in CookieStorage.Cookies)
            CookieStorage.DeleteCookie(cookie);
      }

Xamarin.Forms:

PCL:

IClearCookies.cs

 using System;
 namespace POCDemo
 {
    public interface IClearCookies
     {
        void Clear();
     }
 }

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();
      }
    }
 }

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);
      }
   }
}

Call dependency service

PCL:

DependencyService.Get<IClearCookies>().Clear();

It's working for me

  • This works, but at time of writing, this ONLY works under iOS on the Actual iOS Devices. I tested this code with the iOS 14.5 Simulator (iPhone 12) and Xamarin 16.9 and it did not find any cookies to delete. Running on an iPhone X 14.4.2 it worked just fine. – Tim Hobbs May 14 '21 at 14:15
2

I have had success using these lines of code:

CookieManager.Instance.RemoveAllCookie();
CookieManager.Instance.RemoveSessionCookie();
CookieManager.Instance.Flush();
CookieSyncManager.Instance.Sync();
Mellson
  • 2,918
  • 1
  • 21
  • 23