diff --git a/src/Controls/tests/TestCases.HostApp/Issues/Issue18452.cs b/src/Controls/tests/TestCases.HostApp/Issues/Issue18452.cs new file mode 100644 index 000000000000..676e7fdd6a44 --- /dev/null +++ b/src/Controls/tests/TestCases.HostApp/Issues/Issue18452.cs @@ -0,0 +1,59 @@ +using System.Net; + +namespace Maui.Controls.Sample.Issues +{ + [Issue(IssueTracker.Github, 18452, "NullReferenceException throws on Windows when setting Cookies on .NET MAUI WebView", PlatformAffected.UWP)] + public class Issue18452 : TestContentPage + { + protected override void Init() + { + Grid grid = new Grid(); + grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(0.8, GridUnitType.Star) }); + grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(0.2, GridUnitType.Star) }); + WebView webView = new WebView(); + + Label label = new Label(); + label.AutomationId = "Label"; + + const string url = "https://learn.microsoft.com/en-us/dotnet/"; + + CookieContainer cookieContainer = new(); + Uri uri = new(url, UriKind.RelativeOrAbsolute); + + Cookie cookie = new() + { + Name = "DotNetMAUICookie", + Expires = DateTime.Now.AddDays(1), + Value = "My cookie", + Domain = uri.Host, + Path = "/" + }; + + cookieContainer.Add(uri, cookie); + webView.Cookies = cookieContainer; + webView.AutomationId = "WebView"; + webView.Source = new UrlWebViewSource { Url = uri.ToString() }; + + grid.Children.Add(webView); + + webView.Navigated += (s, e) => + { + var cookies = webView.Cookies.GetCookies(uri); + foreach (Cookie c in cookies) + { + if (c.Name == "DotNetMAUICookie") + { + if (!grid.Contains(label)) + { + grid.Children.Add(label); + label.Text = "Success"; + break; + } + } + } + }; + + Content = grid; + } + } +} diff --git a/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue18452.cs b/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue18452.cs new file mode 100644 index 000000000000..cf68ba1cf1a6 --- /dev/null +++ b/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue18452.cs @@ -0,0 +1,24 @@ +using NUnit.Framework; +using UITest.Appium; +using UITest.Core; + +namespace Microsoft.Maui.TestCases.Tests.Issues; + +[Category(UITestCategories.WebView)] +public class Issue18452 : _IssuesUITest +{ + public override string Issue => "NullReferenceException throws on Windows when setting Cookies on .NET MAUI WebView"; + string? expected = "Success"; + + public Issue18452(TestDevice device) : base(device) + { + } + + [Test] + public void WebViewLoadedWithoutException() + { + App.WaitForElement("Label"); + string? label = App.FindElement("Label").GetText(); + Assert.That(label, Is.EqualTo(expected)); + } +} diff --git a/src/Core/src/Handlers/WebView/WebViewHandler.Windows.cs b/src/Core/src/Handlers/WebView/WebViewHandler.Windows.cs index aca4648bb4b2..ee83f7f6c161 100644 --- a/src/Core/src/Handlers/WebView/WebViewHandler.Windows.cs +++ b/src/Core/src/Handlers/WebView/WebViewHandler.Windows.cs @@ -207,6 +207,11 @@ internal async Task SyncPlatformCookies(string url) if (myCookieJar is null) return; + if (PlatformView.CoreWebView2 is null) + { + return; + } + await InitialCookiePreloadIfNecessary(url); var cookies = myCookieJar.GetCookies(uri); @@ -366,6 +371,10 @@ void OnCoreWebView2Initialized(WebView2 sender, CoreWebView2InitializedEventArgs if (Handler is WebViewHandler handler) { sender.UpdateUserAgent(handler.VirtualView); + if (sender.Source is not null) + { + handler.SyncPlatformCookies(sender.Source.ToString()).FireAndForget(); + } } } @@ -405,4 +414,4 @@ void OnProcessFailed(CoreWebView2 sender, CoreWebView2ProcessFailedEventArgs arg } } } -} \ No newline at end of file +}