How to clear webview cache in .net maui Android?

475 Views Asked by At

I am authenticating through WebView .Net Maui Android, and acquire a token on successful login, I have experienced that on re-login scenarios the authentication details is not requested again, because the browser is storing the previously logged in user. I have to clear the browser’s cookies section to get rid of the useless previous user’s details.

<WebView x:Name="Browser"    WidthRequest="300"  HeightRequest="1000"       
Navigating="Browser_Navigating"     
  Navigated="webOnEndNavigating" />

Xamal.cs

public UAEPass()
    {       
InitializeComponent();        
  Browser.Source = "URL"; 
  
    }
1

There are 1 best solutions below

0
On

There are many threads about how to clear webview cache in the native android on the stackoverflow. You can refer to these cases and convert the code to c#. Such as:

        public void ClearCache()
        {
#if ANDROID
            var webview = Browser.Handler.PlatformView as Android.Webkit.WebView;
            webview.ClearCache(true);
            webview.ClearHistory();
            webview.ClearFormData();
            Android.Webkit.CookieManager.Instance.RemoveAllCookies(null);
            Android.Webkit.CookieManager.Instance.RemoveSessionCookies(null);
            Android.Webkit.WebStorage.Instance.DeleteAllData();
#endif
        }