Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin.Auth, WebView Clear Cookies on Android

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?

like image 794
ethane Avatar asked Jul 01 '17 14:07

ethane


2 Answers

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

like image 133
Venkata Swamy Balaraju Avatar answered Oct 09 '22 00:10

Venkata Swamy Balaraju


I have had success using these lines of code:

CookieManager.Instance.RemoveAllCookie();
CookieManager.Instance.RemoveSessionCookie();
CookieManager.Instance.Flush();
CookieSyncManager.Instance.Sync();
like image 44
Mellson Avatar answered Oct 09 '22 00:10

Mellson