Xamarin.Auth, WebView Clear Cookies on Android

别等时光非礼了梦想. 提交于 2019-12-23 16:15:37

问题


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?


回答1:


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




回答2:


I have had success using these lines of code:

CookieManager.Instance.RemoveAllCookie();
CookieManager.Instance.RemoveSessionCookie();
CookieManager.Instance.Flush();
CookieSyncManager.Instance.Sync();


来源:https://stackoverflow.com/questions/44862027/xamarin-auth-webview-clear-cookies-on-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!