Xamarin.Auth,WebView在Android上清除Cookie [英] Xamarin.Auth, WebView Clear Cookies on Android

查看:246
本文介绍了Xamarin.Auth,WebView在Android上清除Cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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公开了一种清除cookie的方法:

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

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

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.

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

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.

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

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

*编辑
由于CookieManager.Instance.Sync()异步运行,可能是因为它没有及时完成还是根本就没有运行?

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

推荐答案

下面对您有用的代码

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

呼叫依赖服务

PCL:

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

对我有用

这篇关于Xamarin.Auth,WebView在Android上清除Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆