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 ;
25using System ;
36using System . Collections . Generic ;
47using 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 ;
0 commit comments