Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFrameworks>netcoreapp3.1;net5.0-windows;net6.0-windows</TargetFrameworks>
<UseWPF>true</UseWPF>
<Nullable>enable</Nullable>
<Version>1.0.3</Version>
Expand Down Expand Up @@ -29,7 +29,8 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Identity.Client" Version="4.11.0" />
<PackageReference Include="Microsoft.Identity.Client" Version="4.44.0" />
<PackageReference Include="Microsoft.Web.WebView2" Version="1.0.1245.22" />
</ItemGroup>

</Project>
28 changes: 15 additions & 13 deletions EmbeddedMsalCustomWebUi.Wpf/Internal/EmbeddedWebUiWindow.xaml
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
<Window x:Class="EmbeddedMsalCustomWebUi.Wpf.Internal.EmbeddedWebUiWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:EmbeddedMsalCustomWebUi.Wpf.Internal"
mc:Ignorable="d"
WindowStyle="ToolWindow"
Loaded="Window_Loaded"
Closed="Window_Closed"
Title="EmbeddedWebUiWindow" Height="450" Width="800">
<Window
x:Class="EmbeddedMsalCustomWebUi.Wpf.Internal.EmbeddedWebUiWindow"
x:ClassModifier="internal"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:wv="clr-namespace:Microsoft.Web.WebView2.Wpf;assembly=Microsoft.Web.WebView2.Wpf"
Title="EmbeddedWebUiWindow"
Width="800"
Height="450"
Closed="Window_Closed"
Loaded="Window_Loaded"
mc:Ignorable="d">
<Grid>
<WebBrowser x:Name="webBrowser"
Navigating="WebBrowser_Navigating" />
<wv:WebView2 x:Name="webView2" NavigationStarting="webView2_NavigationStarting" />
</Grid>
</Window>
18 changes: 9 additions & 9 deletions EmbeddedMsalCustomWebUi.Wpf/Internal/EmbeddedWebUiWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
using Microsoft.Identity.Client.Extensibility;
using Microsoft.Identity.Client;
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using System.Windows;
using System.Windows.Navigation;

namespace EmbeddedMsalCustomWebUi.Wpf.Internal
{
public partial class EmbeddedWebUiWindow : Window
internal partial class EmbeddedWebUiWindow : Window
{
private readonly Uri _authorizationUri;
private readonly Uri _redirectUri;
Expand All @@ -30,26 +29,27 @@ public EmbeddedWebUiWindow(
_cancellationToken = cancellationToken;
}

private void WebBrowser_Navigating(object sender, NavigatingCancelEventArgs e)
private void webView2_NavigationStarting(object sender, Microsoft.Web.WebView2.Core.CoreWebView2NavigationStartingEventArgs e)
{
if (!e.Uri.ToString().StartsWith(_redirectUri.ToString()))
if (!e.Uri.StartsWith(_redirectUri.ToString()))
{
// not redirect uri case
return;
}

// parse query string
var query = HttpUtility.ParseQueryString(e.Uri.Query);
var uri = new Uri(e.Uri);
var query = HttpUtility.ParseQueryString(uri.Query);
if (query.AllKeys.Any(x => x == "code"))
{
// It has a code parameter.
_taskCompletionSource.SetResult(e.Uri);
_taskCompletionSource.SetResult(uri);
}
else
{
// error.
_taskCompletionSource.SetException(
new MsalExtensionException(
new MsalException(
$"An error occurred, error: {query.Get("error")}, error_description: {query.Get("error_description")}"));
}

Expand All @@ -60,7 +60,7 @@ private void Window_Loaded(object sender, RoutedEventArgs e)
{
_token = _cancellationToken.Register(() => _taskCompletionSource.SetCanceled());
// navigating to an uri that is entry point to authorization flow.
webBrowser.Navigate(_authorizationUri);
webView2.Source = _authorizationUri;
}

private void Window_Closed(object sender, EventArgs e)
Expand Down
17 changes: 8 additions & 9 deletions TestApp/App.xaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
<Application x:Class="TestApp.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TestApp"
StartupUri="MainWindow.xaml"
Startup="Application_Startup">
<Application.Resources>

</Application.Resources>
<Application
x:Class="TestApp.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TestApp"
Startup="Application_Startup"
StartupUri="MainWindow.xaml">
<Application.Resources />
</Application>
10 changes: 5 additions & 5 deletions TestApp/TestApp.csproj
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net6.0-windows</TargetFramework>
<UseWPF>true</UseWPF>
<UserSecretsId>d8844bb0-5042-4342-bccb-764af93cdece</UserSecretsId>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="3.1.3" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="3.1.3" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.5.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="6.0.1" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.19.0" />
</ItemGroup>

<ItemGroup>
Expand Down