Skip to content

Commit 2a9a62b

Browse files
committed
Update to MSAL.NET 3.x
Update the Service nuget packages
1 parent e4e846b commit 2a9a62b

8 files changed

Lines changed: 90 additions & 85 deletions

File tree

TodoListClient/MainWindow.xaml.cs

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public partial class MainWindow : Window
4848

4949

5050
private HttpClient httpClient = new HttpClient();
51-
private PublicClientApplication app = null;
51+
private IPublicClientApplication app = null;
5252

5353
private string[] Scopes = null;
5454

@@ -59,8 +59,11 @@ protected override async void OnInitialized(EventArgs e)
5959
Scopes = new string[] {todoListServiceScope};
6060

6161
// Initialize the PublicClientApplication
62-
app = new PublicClientApplication(clientId, "https://login.microsoftonline.com/common/v2.0", TokenCacheHelper.GetUserCache());
63-
AuthenticationResult result = null;
62+
app = PublicClientApplicationBuilder.Create(clientId)
63+
.Build();
64+
65+
TokenCacheHelper.EnableSerialization(app.UserTokenCache);
66+
6467

6568
// TODO: Check if the user is already signed in.
6669
// As the app starts, we want to check to see if the user is already signed in.
@@ -70,7 +73,8 @@ protected override async void OnInitialized(EventArgs e)
7073
try
7174
{
7275
var accounts = await app.GetAccountsAsync();
73-
result = await app.AcquireTokenSilentAsync(Scopes, accounts.FirstOrDefault());
76+
var result = await app.AcquireTokenSilent(Scopes, accounts.FirstOrDefault())
77+
.ExecuteAsync();
7478
// If we got here, a valid token is in the cache - or MSAL was able to get a new oen via refresh token.
7579
// Proceed to fetch the user's tasks from the TodoListService via the GetTodoList() method.
7680

@@ -121,7 +125,9 @@ private async Task GetTodoList()
121125
// without invoking any UI prompt. AcquireTokenSilentAsync forces
122126
// MSAL to throw an exception if it cannot get a token silently.
123127
var accounts = await app.GetAccountsAsync();
124-
result = await app.AcquireTokenSilentAsync(Scopes, accounts.FirstOrDefault());
128+
result = await app.AcquireTokenSilent(Scopes, accounts.FirstOrDefault())
129+
.ExecuteAsync()
130+
.ConfigureAwait(false);
125131
}
126132
catch (MsalException ex)
127133
{
@@ -186,7 +192,9 @@ private async void AddTodoItem(object sender, RoutedEventArgs e)
186192
try
187193
{
188194
var accounts = await app.GetAccountsAsync();
189-
result = await app.AcquireTokenSilentAsync(Scopes, accounts.FirstOrDefault());
195+
result = await app.AcquireTokenSilent(Scopes, accounts.FirstOrDefault())
196+
.ExecuteAsync()
197+
.ConfigureAwait(false);
190198
}
191199
catch (MsalException ex)
192200
{
@@ -231,11 +239,11 @@ private async void AddTodoItem(object sender, RoutedEventArgs e)
231239
/// <param name="app"></param>
232240
private async Task ClearCache(IPublicClientApplication app)
233241
{
234-
var accounts = await app.GetAccountsAsync();
242+
var accounts = (await app.GetAccountsAsync()).ToList();
235243
while (accounts.Any())
236244
{
237245
await app.RemoveAsync(accounts.First());
238-
accounts = await app.GetAccountsAsync();
246+
accounts = (await app.GetAccountsAsync()).ToList();
239247
}
240248
}
241249
private async void SignIn(object sender = null, RoutedEventArgs args = null)
@@ -261,10 +269,18 @@ private async void SignIn(object sender = null, RoutedEventArgs args = null)
261269
// MSAL will get a token for the TodoListService and cache it for you.
262270

263271
AuthenticationResult result = null;
272+
var accounts = await app.GetAccountsAsync();
264273
try
265274
{
266-
result = await app.AcquireTokenAsync(Scopes);
267-
SignInButton.Content = "Clear Cache";
275+
result = await app.AcquireTokenInteractive(Scopes)
276+
.WithAccount(accounts.FirstOrDefault())
277+
.WithPrompt(Prompt.SelectAccount)
278+
.ExecuteAsync()
279+
.ConfigureAwait(false);
280+
Dispatcher.Invoke(() =>
281+
{
282+
SignInButton.Content = "Clear Cache";
283+
});
268284
GetTodoList();
269285
}
270286
catch (MsalException ex)

TodoListClient/TodoListClient.csproj

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@
3434
<WarningLevel>4</WarningLevel>
3535
</PropertyGroup>
3636
<ItemGroup>
37-
<Reference Include="Microsoft.Identity.Client, Version=2.6.2.0, Culture=neutral, PublicKeyToken=0a613f4dd989e8ae">
38-
<HintPath>..\packages\Microsoft.Identity.Client.2.6.2\lib\net45\Microsoft.Identity.Client.dll</HintPath>
37+
<Reference Include="Microsoft.Identity.Client, Version=3.0.5.0, Culture=neutral, PublicKeyToken=0a613f4dd989e8ae, processorArchitecture=MSIL">
38+
<HintPath>..\packages\Microsoft.Identity.Client.3.0.5-preview\lib\net45\Microsoft.Identity.Client.dll</HintPath>
3939
</Reference>
4040
<Reference Include="System" />
4141
<Reference Include="System.Configuration" />
@@ -63,8 +63,8 @@
6363
<Generator>MSBuild:Compile</Generator>
6464
<SubType>Designer</SubType>
6565
</ApplicationDefinition>
66-
<Compile Include="FileCache.cs" />
6766
<Compile Include="TodoItem.cs" />
67+
<Compile Include="TokenCacheHelper.cs" />
6868
<Page Include="MainWindow.xaml">
6969
<Generator>MSBuild:Compile</Generator>
7070
<SubType>Designer</SubType>
@@ -96,9 +96,7 @@
9696
<Generator>ResXFileCodeGenerator</Generator>
9797
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
9898
</EmbeddedResource>
99-
<None Include="packages.config">
100-
<SubType>Designer</SubType>
101-
</None>
99+
<None Include="packages.config" />
102100
<None Include="Properties\Settings.settings">
103101
<Generator>SettingsSingleFileGenerator</Generator>
104102
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -34,59 +34,45 @@ namespace TodoListClient
3434
static class TokenCacheHelper
3535
{
3636

37-
/// <summary>
38-
/// Get the user token cache
39-
/// </summary>
40-
/// <returns></returns>
41-
public static TokenCache GetUserCache()
42-
{
43-
if (usertokenCache == null)
44-
{
45-
usertokenCache = new TokenCache();
46-
usertokenCache.SetBeforeAccess(BeforeAccessNotification);
47-
usertokenCache.SetAfterAccess(AfterAccessNotification);
48-
}
49-
return usertokenCache;
50-
}
51-
52-
static TokenCache usertokenCache;
53-
5437
/// <summary>
5538
/// Path to the token cache
5639
/// </summary>
57-
public static readonly string CacheFilePath = System.Reflection.Assembly.GetExecutingAssembly().Location + ".msalcache.bin";
40+
private static readonly string CacheFilePath = System.Reflection.Assembly.GetExecutingAssembly().Location + ".msalcache.bin";
5841

5942
private static readonly object FileLock = new object();
6043

61-
public static void BeforeAccessNotification(TokenCacheNotificationArgs args)
44+
private static void BeforeAccessNotification(TokenCacheNotificationArgs args)
6245
{
6346
lock (FileLock)
6447
{
65-
args.TokenCache.Deserialize(File.Exists(CacheFilePath)
48+
args.TokenCache.DeserializeMsalV3(File.Exists(CacheFilePath)
6649
? ProtectedData.Unprotect(File.ReadAllBytes(CacheFilePath),
6750
null,
6851
DataProtectionScope.CurrentUser)
6952
: null);
7053
}
7154
}
7255

73-
public static void AfterAccessNotification(TokenCacheNotificationArgs args)
56+
private static void AfterAccessNotification(TokenCacheNotificationArgs args)
7457
{
7558
// if the access operation resulted in a cache update
76-
if (args.TokenCache.HasStateChanged)
59+
if (args.HasStateChanged)
7760
{
7861
lock (FileLock)
7962
{
80-
// reflect changesgs in the persistent store
63+
// reflect changes in the persistent store
8164
File.WriteAllBytes(CacheFilePath,
82-
ProtectedData.Protect(args.TokenCache.Serialize(),
65+
ProtectedData.Protect(args.TokenCache.SerializeMsalV3(),
8366
null,
8467
DataProtectionScope.CurrentUser)
8568
);
86-
// once the write operationtakes place restore the HasStateChanged bit to filse
87-
args.TokenCache.HasStateChanged = false;
8869
}
8970
}
9071
}
72+
internal static void EnableSerialization(ITokenCache tokenCache)
73+
{
74+
tokenCache.SetBeforeAccess(BeforeAccessNotification);
75+
tokenCache.SetAfterAccess(AfterAccessNotification);
76+
}
9177
}
9278
}

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="2.6.2" allowedVersions="[2,3)" targetFramework="net45" />
3+
<package id="Microsoft.Identity.Client" version="3.0.5-preview" allowedVersions="[3,4)" targetFramework="net45" />
44
</packages>

TodoListService/App_Start/Startup.Auth.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@ public void ConfigureAuth(IAppBuilder app)
2727
new TokenValidationParameters
2828
{
2929
// Check if the audience is intended to be this application
30-
ValidAudience = clientId,
30+
ValidAudiences = new [] { clientId, $"api://{clientId}" },
3131

3232
// Change below to 'true' if you want this Web API to accept tokens issued to one Azure AD tenant only (single-tenant)
33+
// Note that this is a simplification for the quickstart here. You should validate the issuer. For details,
34+
// see https://github.com/Azure-Samples/active-directory-dotnet-native-aspnetcore
3335
ValidateIssuer = false,
3436

3537
},

TodoListService/TodoListService.csproj

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -49,25 +49,20 @@
4949
<Reference Include="Microsoft.IdentityModel.Protocol.Extensions, Version=1.0.40306.1554, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
5050
<HintPath>..\packages\Microsoft.IdentityModel.Protocol.Extensions.1.0.4.403061554\lib\net45\Microsoft.IdentityModel.Protocol.Extensions.dll</HintPath>
5151
</Reference>
52-
<Reference Include="Microsoft.Owin, Version=3.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
53-
<SpecificVersion>False</SpecificVersion>
54-
<HintPath>..\packages\Microsoft.Owin.3.0.1\lib\net45\Microsoft.Owin.dll</HintPath>
52+
<Reference Include="Microsoft.Owin, Version=3.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
53+
<HintPath>..\packages\Microsoft.Owin.3.1.0\lib\net45\Microsoft.Owin.dll</HintPath>
5554
</Reference>
56-
<Reference Include="Microsoft.Owin.Host.SystemWeb, Version=3.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
57-
<SpecificVersion>False</SpecificVersion>
58-
<HintPath>..\packages\Microsoft.Owin.Host.SystemWeb.3.0.1\lib\net45\Microsoft.Owin.Host.SystemWeb.dll</HintPath>
55+
<Reference Include="Microsoft.Owin.Host.SystemWeb, Version=3.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
56+
<HintPath>..\packages\Microsoft.Owin.Host.SystemWeb.3.1.0\lib\net45\Microsoft.Owin.Host.SystemWeb.dll</HintPath>
5957
</Reference>
60-
<Reference Include="Microsoft.Owin.Security, Version=3.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
61-
<SpecificVersion>False</SpecificVersion>
62-
<HintPath>..\packages\Microsoft.Owin.Security.3.0.1\lib\net45\Microsoft.Owin.Security.dll</HintPath>
58+
<Reference Include="Microsoft.Owin.Security, Version=3.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
59+
<HintPath>..\packages\Microsoft.Owin.Security.3.1.0\lib\net45\Microsoft.Owin.Security.dll</HintPath>
6360
</Reference>
64-
<Reference Include="Microsoft.Owin.Security.Jwt, Version=3.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
65-
<SpecificVersion>False</SpecificVersion>
66-
<HintPath>..\packages\Microsoft.Owin.Security.Jwt.3.0.1\lib\net45\Microsoft.Owin.Security.Jwt.dll</HintPath>
61+
<Reference Include="Microsoft.Owin.Security.Jwt, Version=3.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
62+
<HintPath>..\packages\Microsoft.Owin.Security.Jwt.3.1.0\lib\net45\Microsoft.Owin.Security.Jwt.dll</HintPath>
6763
</Reference>
68-
<Reference Include="Microsoft.Owin.Security.OAuth, Version=3.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
69-
<SpecificVersion>False</SpecificVersion>
70-
<HintPath>..\packages\Microsoft.Owin.Security.OAuth.3.0.1\lib\net45\Microsoft.Owin.Security.OAuth.dll</HintPath>
64+
<Reference Include="Microsoft.Owin.Security.OAuth, Version=3.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
65+
<HintPath>..\packages\Microsoft.Owin.Security.OAuth.3.1.0\lib\net45\Microsoft.Owin.Security.OAuth.dll</HintPath>
7166
</Reference>
7267
<Reference Include="Newtonsoft.Json">
7368
<HintPath>..\packages\Newtonsoft.Json.6.0.4\lib\net45\Newtonsoft.Json.dll</HintPath>
@@ -84,8 +79,8 @@
8479
<Reference Include="System.IdentityModel.Tokens.Jwt, Version=4.0.40306.1554, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
8580
<HintPath>..\packages\System.IdentityModel.Tokens.Jwt.4.0.4.403061554\lib\net45\System.IdentityModel.Tokens.Jwt.dll</HintPath>
8681
</Reference>
87-
<Reference Include="System.Net.Http.Formatting, Version=5.2.4.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
88-
<HintPath>..\packages\Microsoft.AspNet.WebApi.Client.5.2.4\lib\net45\System.Net.Http.Formatting.dll</HintPath>
82+
<Reference Include="System.Net.Http.Formatting, Version=5.2.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
83+
<HintPath>..\packages\Microsoft.AspNet.WebApi.Client.5.2.7\lib\net45\System.Net.Http.Formatting.dll</HintPath>
8984
</Reference>
9085
<Reference Include="System.Runtime.Serialization" />
9186
<Reference Include="System.Web.Entity" />
@@ -94,32 +89,32 @@
9489
<Reference Include="System.Core" />
9590
<Reference Include="System.Data.DataSetExtensions" />
9691
<Reference Include="System.Web.Helpers, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
97-
<HintPath>..\packages\Microsoft.AspNet.WebPages.3.2.4\lib\net45\System.Web.Helpers.dll</HintPath>
92+
<HintPath>..\packages\Microsoft.AspNet.WebPages.3.2.7\lib\net45\System.Web.Helpers.dll</HintPath>
9893
</Reference>
99-
<Reference Include="System.Web.Http, Version=5.2.4.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
100-
<HintPath>..\packages\Microsoft.AspNet.WebApi.Core.5.2.4\lib\net45\System.Web.Http.dll</HintPath>
94+
<Reference Include="System.Web.Http, Version=5.2.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
95+
<HintPath>..\packages\Microsoft.AspNet.WebApi.Core.5.2.7\lib\net45\System.Web.Http.dll</HintPath>
10196
</Reference>
102-
<Reference Include="System.Web.Http.WebHost, Version=5.2.4.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
103-
<HintPath>..\packages\Microsoft.AspNet.WebApi.WebHost.5.2.4\lib\net45\System.Web.Http.WebHost.dll</HintPath>
97+
<Reference Include="System.Web.Http.WebHost, Version=5.2.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
98+
<HintPath>..\packages\Microsoft.AspNet.WebApi.WebHost.5.2.7\lib\net45\System.Web.Http.WebHost.dll</HintPath>
10499
</Reference>
105-
<Reference Include="System.Web.Mvc, Version=5.2.4.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
106-
<HintPath>..\packages\Microsoft.AspNet.Mvc.5.2.4\lib\net45\System.Web.Mvc.dll</HintPath>
100+
<Reference Include="System.Web.Mvc, Version=5.2.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
101+
<HintPath>..\packages\Microsoft.AspNet.Mvc.5.2.7\lib\net45\System.Web.Mvc.dll</HintPath>
107102
</Reference>
108103
<Reference Include="System.Web.Optimization, Version=1.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
109104
<SpecificVersion>False</SpecificVersion>
110105
<HintPath>..\packages\Microsoft.AspNet.Web.Optimization.1.1.3\lib\net40\System.Web.Optimization.dll</HintPath>
111106
</Reference>
112107
<Reference Include="System.Web.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
113-
<HintPath>..\packages\Microsoft.AspNet.Razor.3.2.4\lib\net45\System.Web.Razor.dll</HintPath>
108+
<HintPath>..\packages\Microsoft.AspNet.Razor.3.2.7\lib\net45\System.Web.Razor.dll</HintPath>
114109
</Reference>
115110
<Reference Include="System.Web.WebPages, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
116-
<HintPath>..\packages\Microsoft.AspNet.WebPages.3.2.4\lib\net45\System.Web.WebPages.dll</HintPath>
111+
<HintPath>..\packages\Microsoft.AspNet.WebPages.3.2.7\lib\net45\System.Web.WebPages.dll</HintPath>
117112
</Reference>
118113
<Reference Include="System.Web.WebPages.Deployment, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
119-
<HintPath>..\packages\Microsoft.AspNet.WebPages.3.2.4\lib\net45\System.Web.WebPages.Deployment.dll</HintPath>
114+
<HintPath>..\packages\Microsoft.AspNet.WebPages.3.2.7\lib\net45\System.Web.WebPages.Deployment.dll</HintPath>
120115
</Reference>
121116
<Reference Include="System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
122-
<HintPath>..\packages\Microsoft.AspNet.WebPages.3.2.4\lib\net45\System.Web.WebPages.Razor.dll</HintPath>
117+
<HintPath>..\packages\Microsoft.AspNet.WebPages.3.2.7\lib\net45\System.Web.WebPages.Razor.dll</HintPath>
123118
</Reference>
124119
<Reference Include="System.Xml.Linq" />
125120
<Reference Include="System.Web" />

TodoListService/Web.config

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,15 @@
5656
</dependentAssembly>
5757
<dependentAssembly>
5858
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
59-
<bindingRedirect oldVersion="1.0.0.0-5.2.4.0" newVersion="5.2.4.0" />
59+
<bindingRedirect oldVersion="1.0.0.0-5.2.7.0" newVersion="5.2.7.0" />
60+
</dependentAssembly>
61+
<dependentAssembly>
62+
<assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" culture="neutral" />
63+
<bindingRedirect oldVersion="0.0.0.0-3.1.0.0" newVersion="3.1.0.0" />
64+
</dependentAssembly>
65+
<dependentAssembly>
66+
<assemblyIdentity name="Microsoft.Owin.Security" publicKeyToken="31bf3856ad364e35" culture="neutral" />
67+
<bindingRedirect oldVersion="0.0.0.0-3.1.0.0" newVersion="3.1.0.0" />
6068
</dependentAssembly>
6169
</assemblyBinding>
6270
</runtime>

0 commit comments

Comments
 (0)