Skip to content

Commit e2cfcf0

Browse files
committed
Upgrading the sample to MSAL 2.0.0-preview
See https://aka.ms/msal-net-2-released for explanations about the breaking changes in MSAL.NET
1 parent 1ed1076 commit e2cfcf0

3 files changed

Lines changed: 19 additions & 12 deletions

File tree

TodoListClient/MainWindow.xaml.cs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
using System.Linq;
2323
using System.Net.Http;
2424
using System.Net.Http.Headers;
25+
using System.Threading.Tasks;
2526
using System.Web.Script.Serialization;
2627
using System.Windows;
2728

@@ -68,7 +69,8 @@ protected override async void OnInitialized(EventArgs e)
6869
// get a token for the user without showing a UI.
6970
try
7071
{
71-
result = await app.AcquireTokenSilentAsync(Scopes, app.Users.FirstOrDefault());
72+
var accounts = await app.GetAccountsAsync();
73+
result = await app.AcquireTokenSilentAsync(Scopes, accounts.FirstOrDefault());
7274
// If we got here, a valid token is in the cache - or MSAL was able to get a new oen via refresh token.
7375
// Proceed to fetch the user's tasks from the TodoListService via the GetTodoList() method.
7476

@@ -105,7 +107,7 @@ public MainWindow()
105107
InitializeComponent();
106108
}
107109

108-
private async void GetTodoList()
110+
private async Task GetTodoList()
109111
{
110112

111113
// Get a token from MSAL, and attach
@@ -118,8 +120,8 @@ private async void GetTodoList()
118120
// Here, we try to get an access token to call the TodoListService
119121
// without invoking any UI prompt. AcquireTokenSilentAsync forces
120122
// MSAL to throw an exception if it cannot get a token silently.
121-
122-
result = await app.AcquireTokenSilentAsync(Scopes, app.Users.FirstOrDefault());
123+
var accounts = await app.GetAccountsAsync();
124+
result = await app.AcquireTokenSilentAsync(Scopes, accounts.FirstOrDefault());
123125
}
124126
catch (MsalException ex)
125127
{
@@ -183,7 +185,8 @@ private async void AddTodoItem(object sender, RoutedEventArgs e)
183185
AuthenticationResult result = null;
184186
try
185187
{
186-
result = await app.AcquireTokenSilentAsync(Scopes, app.Users.FirstOrDefault());
188+
var accounts = await app.GetAccountsAsync();
189+
result = await app.AcquireTokenSilentAsync(Scopes, accounts.FirstOrDefault());
187190
}
188191
catch (MsalException ex)
189192
{
@@ -226,11 +229,13 @@ private async void AddTodoItem(object sender, RoutedEventArgs e)
226229
/// Clears the cache
227230
/// </summary>
228231
/// <param name="app"></param>
229-
private void ClearCache(IPublicClientApplication app)
232+
private async Task ClearCache(IPublicClientApplication app)
230233
{
231-
foreach(IUser user in app.Users.ToArray())
234+
var accounts = await app.GetAccountsAsync();
235+
while (accounts.Any())
232236
{
233-
app.Remove(user);
237+
await app.RemoveAsync(accounts.First());
238+
accounts = await app.GetAccountsAsync();
234239
}
235240
}
236241
private async void SignIn(object sender = null, RoutedEventArgs args = null)
@@ -245,7 +250,7 @@ private async void SignIn(object sender = null, RoutedEventArgs args = null)
245250
if (SignInButton.Content.ToString() == "Clear Cache")
246251
{
247252
TodoList.ItemsSource = string.Empty;
248-
ClearCache(app);
253+
await ClearCache(app);
249254
SignInButton.Content = "Sign In";
250255
return;
251256
}

TodoListClient/TodoListClient.csproj

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,17 @@
3434
<WarningLevel>4</WarningLevel>
3535
</PropertyGroup>
3636
<ItemGroup>
37-
<Reference Include="Microsoft.Identity.Client, Version=1.1.2.0, Culture=neutral, PublicKeyToken=0a613f4dd989e8ae, processorArchitecture=MSIL">
38-
<HintPath>..\packages\Microsoft.Identity.Client.1.1.2-preview0008\lib\net45\Microsoft.Identity.Client.dll</HintPath>
37+
<Reference Include="Microsoft.Identity.Client, Version=2.0.0.0, Culture=neutral, PublicKeyToken=0a613f4dd989e8ae, processorArchitecture=MSIL">
38+
<HintPath>..\packages\Microsoft.Identity.Client.2.0.0-preview\lib\net45\Microsoft.Identity.Client.dll</HintPath>
3939
</Reference>
4040
<Reference Include="System" />
4141
<Reference Include="System.Configuration" />
4242
<Reference Include="System.Data" />
43+
<Reference Include="System.Drawing" />
4344
<Reference Include="System.Net.Http" />
4445
<Reference Include="System.Security" />
4546
<Reference Include="System.Web.Extensions" />
47+
<Reference Include="System.Windows.Forms" />
4648
<Reference Include="System.Xml" />
4749
<Reference Include="Microsoft.CSharp" />
4850
<Reference Include="System.Core" />

TodoListClient/packages.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
3-
<package id="Microsoft.Identity.Client" version="1.1.2-preview0008" targetFramework="net45" />
3+
<package id="Microsoft.Identity.Client" version="2.0.0-preview" targetFramework="net45" />
44
</packages>

0 commit comments

Comments
 (0)