How to clear WebView cookies in MAUI for windows?

Karthik Nagamani 20 Reputation points
2023-05-19T13:28:58.51+00:00

I am trying to logout our webview data.

Clearing cookies in Windows for MAUI doesn't seems to be working.

I am trying with this code:


Windows.Web.Http.Filters.HttpBaseProtocolFilter myFilter = new global::Windows.Web.Http.Filters.HttpBaseProtocolFilter();
                var cookieManager = myFilter.CookieManager;
HttpCookieCollection myCookieJar = cookieManager.GetCookies(new Uri("url"));
                foreach (HttpCookie cookie in myCookieJar)
                {
                    cookieManager.DeleteCookie(cookie);
                }

I am not getting any cookies count.

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,207 questions
0 comments No comments
{count} votes

Accepted answer
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 29,206 Reputation points Microsoft Vendor
    2023-05-22T02:42:33+00:00

    Hello,

    MAUI that targets Windows platform uses WinUI3, you can try getting the native platform view (WebView2) with WebViewHandler on Windows, then clean cookie by CoreWebView2CookieManager.DeleteAllCookies Method. Please refer to the following code:

    XML

    <StackLayout>  
            <Button Text="ClearCookie" Clicked="Button_Clicked"></Button>  
            <WebView x:Name="MyWebview" Source="XXXX" HeightRequest="400" WidthRequest="400" ></WebView>  
    </StackLayout>
    

    Clicking button to clean cookies.

    private void Button_Clicked(object sender, EventArgs e)
        {
    #if WINDOWS
            var platformView = MyWebview.Handler.PlatformView as WebView2;
            platformView.CoreWebView2.CookieManager.DeleteAllCookies();
    #endif
        }
    

    Best Regards,

    Wenyan Zhang


    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