How to clear webview cache in .net maui Android?

Raju Gadda 66 Reputation points
2023-11-12T17:12:17.1633333+00:00

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" />

 public UAEPass()
 	{ 		
InitializeComponent();        
  Browser.Source = "URL"; 
  
    }
Developer technologies .NET Xamarin
Developer technologies .NET .NET MAUI
Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2023-11-13T05:58:07.9433333+00:00

    Hello,

    I have to clear the browser’s cookies section to get rid of the useless previous user’s details.

    You can do it by following code, you need to clear the cache and delete the all the Application Cache and the HTML5 Web Storage.

    //Clear all the Application Cache, Web SQL Database and the HTML5 Web Storage
    Android.Webkit.WebStorage.Instance.DeleteAllData();
    
    
    // Clear all the cookies
    Android.Webkit.CookieManager.Instance.RemoveAllCookies(null);
    Android.Webkit.CookieManager.Instance.Flush();
    

    If you want to do clear cache at the runtime, you can copy above code to the onCreate method of MainActivity.cs, then you can send messenger to invoke the clear cache code.

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.