Skip to content

Commit

Permalink
Added .NET6.0+ support to Auth0.OidcClient.Winforms (#267)
Browse files Browse the repository at this point in the history
  • Loading branch information
frederikprijck authored Jul 27, 2023
1 parent d262139 commit 4d03d67
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 11 deletions.
6 changes: 6 additions & 0 deletions nuget/Auth0.OidcClient.WinForms.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,19 @@
<dependency id="Auth0.OidcClient.Core" version="3.4.1" />
<dependency id="Microsoft.Toolkit.Forms.UI.Controls.WebView" version="6.1.1"/>
</group>
<group targetFramework="net6.0-windows7.0">
<dependency id="Auth0.OidcClient.Core" version="3.4.1" />
<dependency id="Microsoft.Web.WebView2" version="1.0.1823.32"/>
</group>
</dependencies>
</metadata>
<files>
<file src="..\src\Auth0.OidcClient.WinForms\bin\Release\net462\Auth0.OidcClient.dll" target="lib\net462" />
<file src="..\src\Auth0.OidcClient.WinForms\bin\Release\net462\Auth0.OidcClient.xml" target="lib\net462" />
<file src="..\src\Auth0.OidcClient.WinForms\bin\Release\netcoreapp3.1\Auth0.OidcClient.dll" target="lib\netcoreapp3.1" />
<file src="..\src\Auth0.OidcClient.WinForms\bin\Release\netcoreapp3.1\Auth0.OidcClient.xml" target="lib\netcoreapp3.1" />
<file src="..\src\Auth0.OidcClient.WinForms\bin\Release\net6.0-windows\Auth0.OidcClient.dll" target="lib\net6.0-windows7.0" />
<file src="..\src\Auth0.OidcClient.WinForms\bin\Release\net6.0-windows\Auth0.OidcClient.xml" target="lib\net6.0-windows7.0" />
<file src="..\build\Auth0Icon.png" />
</files>
</package>
23 changes: 16 additions & 7 deletions src/Auth0.OidcClient.WinForms/Auth0.OidcClient.WinForms.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
<PropertyGroup>
<TargetFrameworks>net462;netcoreapp3.1</TargetFrameworks>
<TargetFrameworks>net462;netcoreapp3.1;net6.0-windows</TargetFrameworks>
<OutputType>Library</OutputType>
<RootNamespace>Auth0.OidcClient</RootNamespace>
<AssemblyName>Auth0.OidcClient</AssemblyName>
Expand All @@ -9,14 +9,12 @@
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DefineConstants>TRACE;DEBUG;WINFORMS</DefineConstants>
<TargetFrameworks>net462;netcoreapp3.1</TargetFrameworks>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DefineConstants>TRACE;WINFORMS</DefineConstants>
<DocumentationFile>bin\Release\Auth0.OidcClient.xml</DocumentationFile>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>..\..\build\Auth0OidcClientStrongName.snk</AssemblyOriginatorKeyFile>
<TargetFrameworks>net462;netcoreapp3.1</TargetFrameworks>
</PropertyGroup>
<PropertyGroup>
<ApplicationManifest>app.manifest</ApplicationManifest>
Expand All @@ -28,9 +26,20 @@
<PackageReference Include="IdentityModel.OidcClient">
<Version>5.2.1</Version>
</PackageReference>
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="Microsoft.Toolkit.Forms.UI.Controls.WebView">
<Version>6.1.2</Version>
</PackageReference>
</ItemGroup>
<Choose>
<When Condition="'$(TargetFramework)'!='net6.0-windows'">
<ItemGroup>
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="Microsoft.Toolkit.Forms.UI.Controls.WebView">
<Version>6.1.2</Version>
</PackageReference>
</ItemGroup>
</When>
<When Condition="'$(TargetFramework)'=='net6.0-windows'">
<ItemGroup>
<PackageReference Include="Microsoft.Web.WebView2" Version="1.0.1823.32" />
</ItemGroup>
</When>
</Choose>
</Project>
22 changes: 18 additions & 4 deletions src/Auth0.OidcClient.WinForms/WebViewBrowser.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
using IdentityModel.OidcClient.Browser;
#if NET6_0
using WebViewCompatible = Microsoft.Web.WebView2.WinForms.WebView2;
#else
using Microsoft.Toolkit.Forms.UI.Controls;
#endif
using System;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -42,16 +46,20 @@ public WebViewBrowser(string title = "Authenticating...", int width = 1024, int
}

/// <inheritdoc />
public Task<BrowserResult> InvokeAsync(BrowserOptions options, CancellationToken cancellationToken = default)
public async Task<BrowserResult> InvokeAsync(BrowserOptions options, CancellationToken cancellationToken = default)
{
var tcs = new TaskCompletionSource<BrowserResult>();

var window = _formFactory();
var webView = new WebViewCompatible { Dock = DockStyle.Fill };

webView.NavigationCompleted += (sender, e) =>
webView.NavigationStarting += (sender, e) =>
{
#if NET6_0
if (e.Uri.StartsWith(options.EndUrl))
#else
if (e.Uri.AbsoluteUri.StartsWith(options.EndUrl))
#endif
{
tcs.SetResult(new BrowserResult { ResultType = BrowserResultType.Success, Response = e.Uri.ToString() });
window.Close();
Expand All @@ -64,12 +72,18 @@ public Task<BrowserResult> InvokeAsync(BrowserOptions options, CancellationToken
tcs.SetResult(new BrowserResult { ResultType = BrowserResultType.UserCancel });
};


window.Controls.Add(webView);

window.Show();

#if NET6_0
await webView.EnsureCoreWebView2Async();
webView.CoreWebView2.Navigate(options.StartUrl);
#else
webView.Navigate(options.StartUrl);
#endif

return tcs.Task;
return await tcs.Task;
}
}
}

0 comments on commit 4d03d67

Please sign in to comment.