Skip to content
This repository was archived by the owner on Jun 15, 2026. It is now read-only.

Commit c53493d

Browse files
authored
Merge pull request #58 from Azure-Samples/jennyf/b2c
upgrade to MSAL 4.15 and use new `GetAccounts(userflow)`
2 parents ac77e67 + 79bcb32 commit c53493d

5 files changed

Lines changed: 28 additions & 56 deletions

File tree

active-directory-b2c-wpf/App.xaml.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
using System.IO;
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using System.IO;
25
using System.Text;
36
using System.Windows;
47
using Microsoft.Identity.Client;
@@ -32,7 +35,7 @@ static App()
3235
PublicClientApp = PublicClientApplicationBuilder.Create(ClientId)
3336
.WithB2CAuthority(AuthoritySignUpSignIn)
3437
.WithRedirectUri(RedirectUri)
35-
.WithLogging(Log, LogLevel.Verbose, false) //PiiEnabled set to false
38+
.WithLogging(Log, LogLevel.Info, false) // don't log PII details on a regular basis
3639
.Build();
3740

3841
TokenCacheHelper.Bind(PublicClientApp.UserTokenCache);

active-directory-b2c-wpf/MainWindow.xaml.cs

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
using Microsoft.Identity.Client;
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using Microsoft.Identity.Client;
25
using System;
36
using System.Collections.Generic;
47
using System.Diagnostics;
@@ -22,17 +25,6 @@ public MainWindow()
2225
{
2326
InitializeComponent();
2427
}
25-
26-
private IAccount GetAccountByPolicy(IEnumerable<IAccount> accounts, string policy)
27-
{
28-
foreach (var account in accounts)
29-
{
30-
string accountIdentifier = account.HomeAccountId.ObjectId.Split('.')[0];
31-
if (accountIdentifier.EndsWith(policy.ToLower())) return account;
32-
}
33-
34-
return null;
35-
}
3628

3729
private async void SignInButton_Click(object sender, RoutedEventArgs e)
3830
{
@@ -41,7 +33,7 @@ private async void SignInButton_Click(object sender, RoutedEventArgs e)
4133
try
4234
{
4335
ResultText.Text = "";
44-
authResult = await (app as PublicClientApplication).AcquireTokenInteractive(App.ApiScopes)
36+
authResult = await app.AcquireTokenInteractive(App.ApiScopes)
4537
.WithParentActivityOrWindow(new WindowInteropHelper(this).Handle)
4638
.ExecuteAsync();
4739

@@ -54,7 +46,7 @@ private async void SignInButton_Click(object sender, RoutedEventArgs e)
5446
{
5547
if (ex.Message.Contains("AADB2C90118"))
5648
{
57-
authResult = await (app as PublicClientApplication).AcquireTokenInteractive(App.ApiScopes)
49+
authResult = await app.AcquireTokenInteractive(App.ApiScopes)
5850
.WithParentActivityOrWindow(new WindowInteropHelper(this).Handle)
5951
.WithPrompt(Prompt.SelectAccount)
6052
.WithB2CAuthority(App.AuthorityResetPassword)
@@ -74,6 +66,8 @@ private async void SignInButton_Click(object sender, RoutedEventArgs e)
7466
{
7567
ResultText.Text = $"Error Acquiring Token:{Environment.NewLine}{ex}";
7668
}
69+
70+
DisplayUserInfo(authResult);
7771
}
7872

7973
private async void EditProfileButton_Click(object sender, RoutedEventArgs e)
@@ -83,10 +77,10 @@ private async void EditProfileButton_Click(object sender, RoutedEventArgs e)
8377
{
8478
ResultText.Text = $"Calling API:{App.AuthorityEditProfile}";
8579

86-
AuthenticationResult authResult = await (app as PublicClientApplication).AcquireTokenInteractive(App.ApiScopes)
80+
AuthenticationResult authResult = await app.AcquireTokenInteractive(App.ApiScopes)
8781
.WithParentActivityOrWindow(new WindowInteropHelper(this).Handle)
8882
.WithB2CAuthority(App.AuthorityEditProfile)
89-
.WithPrompt(Prompt.NoPrompt)
83+
.WithPrompt(Prompt.NoPrompt)
9084
.ExecuteAsync(new System.Threading.CancellationToken());
9185

9286
DisplayUserInfo(authResult);
@@ -101,10 +95,10 @@ private async void CallApiButton_Click(object sender, RoutedEventArgs e)
10195
{
10296
AuthenticationResult authResult = null;
10397
var app = App.PublicClientApp;
104-
IEnumerable<IAccount> accounts = await App.PublicClientApp.GetAccountsAsync();
98+
var accounts = await app.GetAccountsAsync(App.PolicySignUpSignIn);
10599
try
106100
{
107-
authResult = await app.AcquireTokenSilent(App.ApiScopes, GetAccountByPolicy(accounts, App.PolicySignUpSignIn))
101+
authResult = await app.AcquireTokenSilent(App.ApiScopes, accounts.FirstOrDefault())
108102
.ExecuteAsync();
109103
}
110104
catch (MsalUiRequiredException ex)
@@ -170,6 +164,7 @@ public async Task<string> GetHttpContentWithToken(string url, string token)
170164

171165
private async void SignOutButton_Click(object sender, RoutedEventArgs e)
172166
{
167+
// SingOut will remove tokens from the token cache from ALL accounts, irrespective of user flow
173168
IEnumerable<IAccount> accounts = await App.PublicClientApp.GetAccountsAsync();
174169
try
175170
{
@@ -192,10 +187,9 @@ private async void Window_Loaded(object sender, RoutedEventArgs e)
192187
try
193188
{
194189
var app = App.PublicClientApp;
195-
IEnumerable<IAccount> accounts = await App.PublicClientApp.GetAccountsAsync();
190+
var accounts = await app.GetAccountsAsync(App.PolicySignUpSignIn);
196191

197-
AuthenticationResult authResult = await app.AcquireTokenSilent(App.ApiScopes,
198-
GetAccountByPolicy(accounts, App.PolicySignUpSignIn))
192+
AuthenticationResult authResult = await app.AcquireTokenSilent(App.ApiScopes, accounts.FirstOrDefault())
199193
.ExecuteAsync();
200194

201195
DisplayUserInfo(authResult);
@@ -237,11 +231,11 @@ private void UpdateSignInState(bool signedIn)
237231

238232
private void DisplayUserInfo(AuthenticationResult authResult)
239233
{
240-
TokenInfoText.Text = "";
241234
if (authResult != null)
242235
{
243236
JObject user = ParseIdToken(authResult.IdToken);
244237

238+
TokenInfoText.Text = "";
245239
TokenInfoText.Text += $"Name: {user["name"]?.ToString()}" + Environment.NewLine;
246240
TokenInfoText.Text += $"User Identifier: {user["oid"]?.ToString()}" + Environment.NewLine;
247241
TokenInfoText.Text += $"Street Address: {user["streetAddress"]?.ToString()}" + Environment.NewLine;

active-directory-b2c-wpf/TokenCacheHelper.cs

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,8 @@
1-
//------------------------------------------------------------------------------
2-
//
3-
// Copyright (c) Microsoft Corporation.
4-
// All rights reserved.
5-
//
6-
// This code is licensed under the MIT License.
7-
//
8-
// Permission is hereby granted, free of charge, to any person obtaining a copy
9-
// of this software and associated documentation files(the "Software"), to deal
10-
// in the Software without restriction, including without limitation the rights
11-
// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
12-
// copies of the Software, and to permit persons to whom the Software is
13-
// furnished to do so, subject to the following conditions :
14-
//
15-
// The above copyright notice and this permission notice shall be included in
16-
// all copies or substantial portions of the Software.
17-
//
18-
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19-
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20-
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
21-
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22-
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23-
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24-
// THE SOFTWARE.
25-
//
26-
//------------------------------------------------------------------------------
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
273

284
using System.IO;
295
using System.Security.Cryptography;
30-
using System.Windows.Controls;
316
using Microsoft.Identity.Client;
327

338
namespace active_directory_b2c_wpf

active-directory-b2c-wpf/active-directory-b2c-wpf.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@
3535
<WarningLevel>4</WarningLevel>
3636
</PropertyGroup>
3737
<ItemGroup>
38-
<Reference Include="Microsoft.Identity.Client, Version=4.13.0.0, Culture=neutral, PublicKeyToken=0a613f4dd989e8ae, processorArchitecture=MSIL">
39-
<HintPath>..\packages\Microsoft.Identity.Client.4.13.0\lib\net45\Microsoft.Identity.Client.dll</HintPath>
38+
<Reference Include="Microsoft.Identity.Client, Version=4.15.0.0, Culture=neutral, PublicKeyToken=0a613f4dd989e8ae, processorArchitecture=MSIL">
39+
<HintPath>..\packages\Microsoft.Identity.Client.4.15.0\lib\net45\Microsoft.Identity.Client.dll</HintPath>
4040
</Reference>
4141
<Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
4242
<HintPath>..\packages\Newtonsoft.Json.12.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
@@ -107,4 +107,4 @@
107107
<None Include="App.config" />
108108
</ItemGroup>
109109
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
110-
</Project>
110+
</Project>
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
3-
<package id="Microsoft.Identity.Client" version="4.13.0" targetFramework="net472" />
3+
<package id="Microsoft.Identity.Client" version="4.15.0" targetFramework="net472" />
44
<package id="Microsoft.NETCore.Platforms" version="1.1.1" targetFramework="net452" />
55
<package id="Microsoft.NETCore.Targets" version="1.1.3" targetFramework="net452" />
66
<package id="Newtonsoft.Json" version="12.0.2" targetFramework="net452" />
77
<package id="System.Private.Uri" version="4.3.2" targetFramework="net452" />
8-
</packages>
8+
</packages>

0 commit comments

Comments
 (0)