I’m using Maui webview to show my local angular app on http://192.168.1.X:4200
The Angular app in question uses .net core APIs hosted locally at https://192.168.1.Y/nameofapis
Starting my Maui app, I can show the Angular site, but every http request made to the service gives me the error:
net::ERR_CERT_AUTHORITY_INVALID
Usually when I use the Angular app directly from the browser (not from Maui), to solve this problem I just need to navigate to the service URL (https://192.168.1.Y/nameofapis) and authorize the self-signed certificate.
But I can’t do this from the maui webview.
This is my code:
MainPage.xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="test.MainPage"
xmlns:controls="clr-namespace:test.Controls">
<ScrollView>
<controls:CustomWebView x:Name="MyWebView" Source="http://192.168.1.X:4200">
</controls:CustomWebView>
</ScrollView>
</ContentPage>
MainPage.xaml.cs
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
}
CustomWebViewRenderer.cs
[assembly: ExportRenderer(typeof(CustomWebView), typeof(CustomWebViewRenderer))]
namespace test.Platforms.Android
{
public class CustomWebViewRenderer : WebViewRenderer
{
public CustomWebViewRenderer(Context context) : base(context)
{
}
protected override void OnElementChanged(ElementChangedEventArgs<Microsoft.Maui.Controls.WebView> e)
{
base.OnElementChanged(e);
Control.SetWebViewClient(new CustomWebViewClient());
global::Android.Webkit.WebView.SetWebContentsDebuggingEnabled(true);
}
}
public class CustomWebViewClient : global::Android.Webkit.WebViewClient
{
public CustomWebViewClient()
{
}
public override void OnReceivedSslError(global::Android.Webkit.WebView view, SslErrorHandler handler, SslError error)
{
handler.Proceed();//this line make ssl error handle so the webview show the page even with certificate errors
}
}
}
CustomWebView.cs
public class CustomWebView : WebView
{
public CustomWebView()
{
}
}
How can I solve the problem?