step-up-labs / firebase-authentication-dotnet

C# library for Firebase Authentication
MIT License
382 stars 130 forks source link

UI .Net 5.0 WebView2 error #183

Closed nelsonhp3 closed 1 year ago

nelsonhp3 commented 1 year ago

When building Auth.WPF.Sample with .Net 5.0 it gives this error: Microsoft.Toolkit.Wpf.UI.Controls.WebView.dll cannot be referenced because it uses built-in support for WinRT, which is no longer supported in .NET 5 and higher. An updated version of the component supporting .NET 5 is needed

I've changed the NuGet package Microsoft.Toolkit.Forms.UI.Controls.WebView to Microsoft.Web.WebView2 and made these changes:

<Window x:Class="Firebase.Auth.UI.WebAuthenticationBrokerWindow"
        xmlns:web="clr-namespace:Microsoft.Web.WebView2.Wpf;assembly=Microsoft.Web.WebView2.Wpf"
        ... >
    <Grid>
        <web:WebView2 x:Name="WebView" />
    </Grid>
</Window>
internal static class WebAuthenticationBroker
    {
        public static Task<string> AuthenticateAsync(Window owner, FirebaseProviderType provider, string uri, string redirectUri)
        {
            var tcs = new TaskCompletionSource<string>();

            Application.Current.Dispatcher.Invoke(() =>
            {
                var window = new WebAuthenticationBrokerWindow();
                window.WebView.NavigationCompleted += (s, e) =>
                {
                    if ((s as WebView2).Source.ToString().StartsWith(redirectUri))
                    {
                        tcs.SetResult((s as WebView2).Source.ToString());
                        window.DialogResult = true;
                        window.Close();
                    }
                };
                window.Title = ProviderToTitleConverter.Convert(provider);
                window.WebView.Loaded += async (sender, e) =>
                {
                    await window.WebView.EnsureCoreWebView2Async().ConfigureAwait(true);
                    window.WebView.NavigateToString(uri);
                };
                window.Owner = owner;
                if (!(window.ShowDialog() ?? false))
                {
                    tcs.SetResult(null);
                }
            });

            return tcs.Task;
        }
    }

And this was the result: image

What can we make to Auth.UI.WPF work with .Net5.0?

bezysoftware commented 1 year ago

Given .NET 5 is no longer supported (libs target .NET 6, which is LTS) I leave it to others to comment / investigate.

I'm not sure I understand what you change the nuget reference to, the lib now uses Microsoft.Web.WebView2, previously it used Microsoft.Toolkit.Wpf.UI.Controls.WebView which is no longer maintained

nelsonhp3 commented 1 year ago

Sorry, I didn't see the #180 commit🤦‍♂️ But now I'm having this issue: image

nelsonhp3 commented 1 year ago

I just restarted Visual Studio and now it's working. But i'm still wonder about this error.