How to remove session in WebView when App is closed?

David 356 Reputation points
2021-04-26T10:13:06.217+00:00

Hi,

I have found the WebView session cookies are still there when I sign out of a website o or close the mobile app. Is there a way to clear them?

Thanks

Developer technologies .NET Xamarin
0 comments No comments
{count} votes

Accepted answer
  1. JarvanZhang 23,971 Reputation points
    2021-04-26T11:37:53.19+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    Hi, David. How did you remove the cookies in your project? To remove the cookies in Xamarin.Forms, please achieve the function on each platform. You could call the function code in the shared project using DependencySerive. Here is the related code, you could refer to:

    1.Create an interface for the native platform functionality in the shared project.

       namespace TestApplication  
       {  
           public interface IClearCookiesInterface  
           {  
               void ClearAllCookies();  
           }  
       }  
    

    2.Implement the interface in the required platform projects and register the platform implementations.

       //android  
       [assembly: Xamarin.Forms.Dependency(typeof(DroidImplementation))]  
       namespace TestApplication.Droid  
       {  
           public class DroidImplementation : IClearCookiesInterface  
           {  
               public void ClearAllCookies()  
               {  
                   var cookieManager = CookieManager.Instance;  
                   cookieManager.RemoveAllCookie();  
               }  
           }  
       }  
       //ios  
       [assembly: Dependency(typeof(IOSImplementation))]    
       namespace TestApplication.iOS  
       {  
           public class IOSImplementation : IClearCookiesInterface  
           {  
               public void ClearAllCookies()  
               {  
                   NSHttpCookieStorage CookieStorage = NSHttpCookieStorage.SharedStorage;  
                   foreach (var cookie in CookieStorage.Cookies)  
                   {   
                       CookieStorage.DeleteCookie(cookie);  
                   }  
               }  
           }  
       }  
    

    3.Invoke the function code in the shared project.

       DependencyService.Get<IClearCookiesInterface>().ClearAllCookies();  
    

    Best Regards,

    Jarvan Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    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.

    1 person found this answer helpful.

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.