Skip to content

Commit d5325cf

Browse files
authored
Merge pull request #60 from Azure-Samples/jmprieur/msal4-20
Demonstrate usage of WAM (and update to MSAL 4.23)
2 parents 0c429a8 + 05b875d commit d5325cf

File tree

7 files changed

+62
-16
lines changed

7 files changed

+62
-16
lines changed
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
<?xml version="1.0" encoding="utf-8" ?>
1+
<?xml version="1.0" encoding="utf-8"?>
22
<configuration>
33
<startup>
4-
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1"/>
55
</startup>
6-
</configuration>
6+
</configuration>

active-directory-wpf-msgraph-v2/App.xaml.cs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,33 @@
11
using Microsoft.Identity.Client;
2+
using System.Linq.Expressions;
23
using System.Windows;
34

45
namespace active_directory_wpf_msgraph_v2
56
{
67
/// <summary>
78
/// Interaction logic for App.xaml
89
/// </summary>
9-
10+
1011
// To change from Microsoft public cloud to a national cloud, use another value of AzureCloudInstance
1112
public partial class App : Application
1213
{
1314
static App()
1415
{
15-
_clientApp = PublicClientApplicationBuilder.Create(ClientId)
16+
CreateApplication(true);
17+
}
18+
19+
public static void CreateApplication(bool useWam)
20+
{
21+
var builder = PublicClientApplicationBuilder.Create(ClientId)
1622
.WithAuthority($"{Instance}{Tenant}")
17-
.WithDefaultRedirectUri()
18-
.Build();
23+
.WithDefaultRedirectUri();
24+
25+
if (useWam)
26+
{
27+
builder.WithExperimentalFeatures();
28+
builder.WithBroker(true); // Requires redirect URI "ms-appx-web://microsoft.aad.brokerplugin/{client_id}" in app registration
29+
}
30+
_clientApp = builder.Build();
1931
TokenCacheHelper.EnableSerialization(_clientApp.UserTokenCache);
2032
}
2133

@@ -32,7 +44,7 @@ static App()
3244
// Note: Tenant is important for the quickstart.
3345
private static string Tenant = "common";
3446
private static string Instance = "https://login.microsoftonline.com/";
35-
private static IPublicClientApplication _clientApp ;
47+
private static IPublicClientApplication _clientApp;
3648

3749
public static IPublicClientApplication PublicClientApp { get { return _clientApp; } }
3850
}

active-directory-wpf-msgraph-v2/MainWindow.xaml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,15 @@
55
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
66
xmlns:local="clr-namespace:active_directory_wpf_msgraph_v2"
77
mc:Ignorable="d"
8-
Title="MainWindow" Height="Auto" Width="525" SizeToContent="Height">
8+
Title="MainWindow" Height="Auto" Width="600" SizeToContent="Height">
99
<Grid>
1010
<StackPanel Background="Azure">
1111
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
12+
<ComboBox x:Name="howToSignIn" SelectedIndex="0" SelectionChanged="UseWam_Changed" VerticalAlignment="Center">
13+
<ComboBoxItem>Use account used to signed-in in Windows (WAM)</ComboBoxItem>
14+
<ComboBoxItem>Use one of the Accounts known by Windows (WAM)</ComboBoxItem>
15+
<ComboBoxItem>Use any account (Azure AD)</ComboBoxItem>
16+
</ComboBox>
1217
<Button x:Name="CallGraphButton" Content="Call Microsoft Graph API" HorizontalAlignment="Right" Padding="5" Click="CallGraphButton_Click" Margin="5" FontFamily="Segoe Ui"/>
1318
<Button x:Name="SignOutButton" Content="Sign-Out" HorizontalAlignment="Right" Padding="5" Click="SignOutButton_Click" Margin="5" Visibility="Collapsed" FontFamily="Segoe Ui"/>
1419
</StackPanel>

active-directory-wpf-msgraph-v2/MainWindow.xaml.cs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,30 @@ private async void CallGraphButton_Click(object sender, RoutedEventArgs e)
3737
ResultText.Text = string.Empty;
3838
TokenInfoText.Text = string.Empty;
3939

40-
var accounts = await app.GetAccountsAsync();
41-
var firstAccount = accounts.FirstOrDefault();
40+
IAccount firstAccount;
41+
42+
switch(howToSignIn.SelectedIndex)
43+
{
44+
// 0: Use account used to signed-in in Windows (WAM)
45+
case 0:
46+
// WAM will always get an account in the cache. So if we want
47+
// to have a chance to select the accounts interactively, we need to
48+
// force the non-account
49+
firstAccount = PublicClientApplication.OperatingSystemAccount;
50+
break;
51+
52+
// 1: Use one of the Accounts known by Windows(WAM)
53+
case 1:
54+
// We force WAM to display the dialog with the accounts
55+
firstAccount = null;
56+
break;
57+
58+
// Use any account(Azure AD). It's not using WAM
59+
default:
60+
var accounts = await app.GetAccountsAsync();
61+
firstAccount = accounts.FirstOrDefault();
62+
break;
63+
}
4264

4365
try
4466
{
@@ -137,5 +159,11 @@ private void DisplayBasicTokenInfo(AuthenticationResult authResult)
137159
TokenInfoText.Text += $"Token Expires: {authResult.ExpiresOn.ToLocalTime()}" + Environment.NewLine;
138160
}
139161
}
162+
163+
private void UseWam_Changed(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
164+
{
165+
SignOutButton_Click(sender, e);
166+
App.CreateApplication(howToSignIn.SelectedIndex != 2); // Not Azure AD accounts (that is use WAM accounts)
167+
}
140168
}
141169
}

active-directory-wpf-msgraph-v2/Properties/Resources.Designer.cs

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

active-directory-wpf-msgraph-v2/Properties/Settings.Designer.cs

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

active-directory-wpf-msgraph-v2/active-directory-wpf-msgraph-v2.csproj

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@
88
<OutputType>WinExe</OutputType>
99
<RootNamespace>active_directory_wpf_msgraph_v2</RootNamespace>
1010
<AssemblyName>active_directory_wpf_msgraph_v2</AssemblyName>
11-
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
11+
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
1212
<FileAlignment>512</FileAlignment>
1313
<ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
1414
<WarningLevel>4</WarningLevel>
1515
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
16+
<TargetFrameworkProfile />
1617
</PropertyGroup>
1718
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
1819
<PlatformTarget>AnyCPU</PlatformTarget>
@@ -100,8 +101,8 @@
100101
</ItemGroup>
101102
<ItemGroup>
102103
<PackageReference Include="Microsoft.Identity.Client">
103-
<Version>4.22.0</Version>
104+
<Version>4.23.0</Version>
104105
</PackageReference>
105106
</ItemGroup>
106107
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
107-
</Project>
108+
</Project>

0 commit comments

Comments
 (0)