This repository was archived by the owner on May 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Update to msal v2.0.1-preview #3
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,41 +36,36 @@ public MainPage() | |
private async void SignInButton_Click(object sender, RoutedEventArgs e) | ||
{ | ||
AuthenticationResult authResult = null; | ||
IEnumerable<IAccount> accounts = await App.PublicClientApp.GetAccountsAsync(); | ||
try | ||
{ | ||
authResult = await App.PublicClientApp.AcquireTokenAsync(App.ApiScopes, GetUserByPolicy(App.PublicClientApp.Users, App.PolicySignUpSignIn), UIBehavior.SelectAccount, string.Empty, null, App.Authority); | ||
IAccount currentUserAccount = GetUserByPolicy(accounts, App.PolicySignUpSignIn); | ||
authResult = await App.PublicClientApp.AcquireTokenSilentAsync(App.ApiScopes, currentUserAccount, App.Authority, false); | ||
|
||
DisplayBasicTokenInfo(authResult); | ||
UpdateSignInState(true); | ||
} | ||
catch (MsalServiceException ex) | ||
catch (MsalUiRequiredException ex) | ||
{ | ||
try | ||
{ | ||
if (ex.Message.Contains("AADB2C90118")) | ||
{ | ||
authResult = await App.PublicClientApp.AcquireTokenAsync(App.ApiScopes, GetUserByPolicy(App.PublicClientApp.Users, App.PolicySignUpSignIn), UIBehavior.SelectAccount, string.Empty, null, App.AuthorityResetPassword); | ||
} | ||
else | ||
{ | ||
ResultText.Text = $"Error Acquiring Token:{Environment.NewLine}{ex}"; | ||
} | ||
} | ||
catch (Exception) | ||
{ | ||
} | ||
authResult = await App.PublicClientApp.AcquireTokenAsync(App.ApiScopes, GetUserByPolicy(accounts, App.PolicySignUpSignIn), UIBehavior.SelectAccount, string.Empty, null, App.Authority); | ||
DisplayBasicTokenInfo(authResult); | ||
UpdateSignInState(true); | ||
} | ||
|
||
catch (Exception ex) | ||
{ | ||
ResultText.Text = $"Users:{string.Join(",", App.PublicClientApp.Users.Select(u => u.Identifier))}{Environment.NewLine}Error Acquiring Token:{Environment.NewLine}{ex}"; | ||
ResultText.Text = $"Users:{string.Join(",", accounts.Select(u => u.Username))}{Environment.NewLine}Error Acquiring Token:{Environment.NewLine}{ex}"; | ||
} | ||
} | ||
|
||
|
||
private async void EditProfileButton_Click(object sender, RoutedEventArgs e) | ||
{ | ||
try | ||
{ | ||
IEnumerable<IAccount> accounts = await App.PublicClientApp.GetAccountsAsync(); | ||
ResultText.Text = $"Calling API:{App.AuthorityEditProfile}"; | ||
AuthenticationResult authResult = await App.PublicClientApp.AcquireTokenAsync(App.ApiScopes, GetUserByPolicy(App.PublicClientApp.Users, App.PolicyEditProfile), UIBehavior.SelectAccount, string.Empty, null, App.AuthorityEditProfile); | ||
AuthenticationResult authResult = await App.PublicClientApp.AcquireTokenAsync(App.ApiScopes, GetUserByPolicy(accounts, App.PolicyEditProfile), UIBehavior.SelectAccount, string.Empty, null, App.AuthorityEditProfile); | ||
DisplayBasicTokenInfo(authResult); | ||
} | ||
catch (Exception ex) | ||
|
@@ -82,9 +77,11 @@ private async void EditProfileButton_Click(object sender, RoutedEventArgs e) | |
private async void CallApiButton_Click(object sender, RoutedEventArgs e) | ||
{ | ||
AuthenticationResult authResult = null; | ||
IEnumerable<IAccount> accounts = await App.PublicClientApp.GetAccountsAsync(); | ||
try | ||
{ | ||
authResult = await App.PublicClientApp.AcquireTokenSilentAsync(App.ApiScopes, GetUserByPolicy(App.PublicClientApp.Users, App.PolicySignUpSignIn), App.Authority, false); | ||
|
||
authResult = await App.PublicClientApp.AcquireTokenSilentAsync(App.ApiScopes, GetUserByPolicy(accounts, App.PolicySignUpSignIn), App.Authority, false); | ||
} | ||
catch (MsalUiRequiredException ex) | ||
{ | ||
|
@@ -93,7 +90,7 @@ private async void CallApiButton_Click(object sender, RoutedEventArgs e) | |
|
||
try | ||
{ | ||
authResult = await App.PublicClientApp.AcquireTokenAsync(App.ApiScopes, GetUserByPolicy(App.PublicClientApp.Users, App.PolicySignUpSignIn)); | ||
authResult = await App.PublicClientApp.AcquireTokenAsync(App.ApiScopes, GetUserByPolicy(accounts, App.PolicySignUpSignIn)); | ||
} | ||
catch (MsalException msalex) | ||
{ | ||
|
@@ -137,22 +134,25 @@ public async Task<string> GetHttpContentWithToken(string url, string token) | |
} | ||
} | ||
|
||
private void SignOutButton_Click(object sender, RoutedEventArgs e) | ||
private async void SignOutButton_Click(object sender, RoutedEventArgs e) | ||
{ | ||
if (App.PublicClientApp.Users.Any()) | ||
IEnumerable<IAccount> accounts = await App.PublicClientApp.GetAccountsAsync(); | ||
|
||
try | ||
{ | ||
try | ||
while (accounts.Any()) | ||
{ | ||
foreach (var user in App.PublicClientApp.Users) | ||
{ | ||
App.PublicClientApp.Remove(user); | ||
} | ||
UpdateSignInState(false); | ||
} | ||
catch (MsalException ex) | ||
{ | ||
ResultText.Text = $"Error signing-out user: {ex.Message}"; | ||
await App.PublicClientApp.RemoveAsync(accounts.FirstOrDefault()); | ||
accounts = await App.PublicClientApp.GetAccountsAsync(); | ||
} | ||
|
||
|
||
UpdateSignInState(false); | ||
|
||
} | ||
catch (MsalException ex) | ||
{ | ||
ResultText.Text = $"Error signing-out user: {ex.Message}"; | ||
} | ||
} | ||
|
||
|
@@ -184,17 +184,21 @@ private void DisplayBasicTokenInfo(AuthenticationResult authResult) | |
TokenInfoText.Text = ""; | ||
if (authResult != null) | ||
{ | ||
TokenInfoText.Text += $"Name: {authResult.User.Name}" + Environment.NewLine; | ||
TokenInfoText.Text += $"Name: {authResult.Account.Username}" + Environment.NewLine; | ||
TokenInfoText.Text += $"Token Expires: {authResult.ExpiresOn.ToLocalTime()}" + Environment.NewLine; | ||
TokenInfoText.Text += $"Access Token: {authResult.AccessToken}" + Environment.NewLine; | ||
TokenInfoText.Text += $"Id Token: {authResult.IdToken}" + Environment.NewLine; | ||
TokenInfoText.Text += $"Tenant Id: {authResult.TenantId}" + Environment.NewLine; | ||
} | ||
} | ||
|
||
private async void Window_Loaded(object sender, RoutedEventArgs e) | ||
{ | ||
try | ||
{ | ||
AuthenticationResult authResult = await App.PublicClientApp.AcquireTokenSilentAsync(App.ApiScopes, GetUserByPolicy(App.PublicClientApp.Users, App.PolicySignUpSignIn), App.Authority, true); | ||
IEnumerable<IAccount> accounts = await App.PublicClientApp.GetAccountsAsync(); | ||
|
||
AuthenticationResult authResult = await App.PublicClientApp.AcquireTokenSilentAsync(App.ApiScopes, GetUserByPolicy(accounts, App.PolicySignUpSignIn), App.Authority, true); | ||
DisplayBasicTokenInfo(authResult); | ||
UpdateSignInState(true); | ||
} | ||
|
@@ -212,12 +216,12 @@ private async void Window_Loaded(object sender, RoutedEventArgs e) | |
} | ||
} | ||
|
||
private IUser GetUserByPolicy(IEnumerable<IUser> users, string policy) | ||
private IAccount GetUserByPolicy(IEnumerable<IAccount> accounts, string policy) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
rename: GetAccountByPolicy There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done. |
||
{ | ||
foreach (var user in users) | ||
foreach (var account in accounts) | ||
{ | ||
string userIdentifier = Base64UrlDecode(user.Identifier.Split('.')[0]); | ||
if (userIdentifier.EndsWith(policy.ToLower())) return user; | ||
string userIdentifier = account.HomeAccountId.ObjectId.Split('.')[0]; | ||
if (userIdentifier.EndsWith(policy.ToLower())) return account; | ||
} | ||
|
||
return null; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
{ | ||
"dependencies": { | ||
"Microsoft.Identity.Client": "1.1.0-preview", | ||
"Microsoft.Identity.Client": "2.0.1-preview", | ||
"Microsoft.NETCore.UniversalWindowsPlatform": "5.2.3" | ||
}, | ||
"frameworks": { | ||
"uap10.0": {} | ||
"uap10.0.10586": {} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
is this needed? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it's not...removed |
||
}, | ||
"runtimes": { | ||
"win10-arm": {}, | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Catching the uirequired exception here.
The call api still shows "unauthorized"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you get unauthorized for all account types? Are you requesting an AT for the expected scope? (or is the scope/resource) accessed different from the one the AT is acquired for?
In reply to: 217924219 [](ancestors = 217924219)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we've been having issues with this. i'll sync with Parakh and see if we can figure it out. might be a settings issue.