Hamata6
Hamata6

Reputation: 55

MAUI app with WebView not remembering cookies on Android

I have a (.NET 7) MAUI app that shows a WebView where users can log in. On Android, it seems that cookies (or at least the session cookie) are not remembered when the user logs in with the “Remember me” option activated: When I close the app (also from the recent apps screen), wait some seconds and then reopen the app, the user has to log in again.

I use a custom WebViewClient (because of the ShouldOverrideUrlLoading) that is set in the OnElementChanged of the WebViewRenderer:

protected override void OnElementChanged(ElementChangedEventArgs<Microsoft.Maui.Controls.WebView> e)
    {
        base.OnElementChanged(e);
        Control.SetWebViewClient(new CustomWebViewClientAndroid(Control.WebViewClient));
    }

In that CustomWebViewClient, I inject the original one to override all other (not needed) methods and preserve the behavior of MAUI:

public class CustomWebViewClientAndroid : WebViewClient
{
    private WebViewClient _defaultClient;

    public CustomWebViewClientAndroid(WebViewClient defaultClient) : base()
    {
        if (defaultClient == null)
            throw new ArgumentNullException(nameof(defaultClient));
        _defaultClient = defaultClient;
    }

    public override bool ShouldOverrideUrlLoading(global::Android.Webkit.WebView? view, IWebResourceRequest? request)
    {
        var cancel = App.RootPage.Navigate(request.Url.ToString());
        if (!cancel)
            return _defaultClient.ShouldOverrideUrlLoading(view, request);
        return cancel;
    }

    #region Default overrides (without custom implementation)
    public override bool ShouldOverrideKeyEvent(global::Android.Webkit.WebView? view, KeyEvent? e)
    {
        return _defaultClient.ShouldOverrideKeyEvent(view, e);
    }

    // All other possible overrides, implemented just like above
}

Do I need to handle the save of the cookies myself or is this a bug with MAUI or Android Webview? I expect the OS (Android) to handle the cookie store part.

Upvotes: 4

Views: 1713

Answers (1)

Liyun Zhang - MSFT
Liyun Zhang - MSFT

Reputation: 14509

First of all, you can try to add the following code in the OnElementChanged method.

protected override void OnElementChanged(ElementChangedEventArgs<Microsoft.Maui.Controls.WebView> e)
    {
        base.OnElementChanged(e);
        Control.SetWebViewClient(new CustomWebViewClientAndroid(Control.WebViewClient));
        Control.Settings.CacheMode = CacheModes.CacheElseNetwork;
        CookieManager.Instance.AcceptThirdPartyCookies(Control);
        CookieManager.Instance.SetAcceptThirdPartyCookies(Control, true);
        CookieManager.Instance.SetAcceptCookie(true);
    }

In addition, you can check the official document about Setting cookies for the webview in the maui Android to set the cookie for the website you want.

Finally, I sugget you use the custom handler instead of the custom renderer in the .net maui. And you can put the code of the renderer's OnElementChanged method into the handler's ConnectHandler method.

For the detail steps, you can refer to the official wiki about Porting Custom Renderers to Handlers on the github.

Upvotes: 2

Related Questions