Skip to content
This repository was archived by the owner on May 2, 2023. It is now read-only.

Update to msal v2.0.1-preview #3

Merged
merged 2 commits into from
Sep 17, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions active-directory-b2c-dotnet-uwp/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,16 +104,14 @@ private void OnSuspending(object sender, SuspendingEventArgs e)
public static string PolicyEditProfile = "b2c_1_edit_profile";
public static string PolicyResetPassword = "b2c_1_reset";

public static string[] ApiScopes = { "https://fabrikamb2c.onmicrosoft.com/demoapi/demo.read" };
public static string[] ApiScopes = { "https://fabrikamb2c.onmicrosoft.com/demoapi/demo.read"};
public static string ApiEndpoint = "https://fabrikamb2chello.azurewebsites.net/hello";

private static string BaseAuthority = "https://login.microsoftonline.com/tfp/{tenant}/{policy}/oauth2/v2.0/authorize";
public static string Authority = BaseAuthority.Replace("{tenant}", Tenant).Replace("{policy}", PolicySignUpSignIn);
public static string AuthorityEditProfile = BaseAuthority.Replace("{tenant}", Tenant).Replace("{policy}", PolicyEditProfile);
public static string AuthorityResetPassword = BaseAuthority.Replace("{tenant}", Tenant).Replace("{policy}", PolicyResetPassword);

private static PublicClientApplication _clientApp = new PublicClientApplication(ClientId, Authority);

public static PublicClientApplication PublicClientApp { get { return _clientApp; } }
public static PublicClientApplication PublicClientApp { get; } = new PublicClientApplication(ClientId, Authority);
}
}
80 changes: 42 additions & 38 deletions active-directory-b2c-dotnet-uwp/MainPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Contributor Author

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"

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)

Copy link
Contributor Author

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.


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)
Expand All @@ -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)
{
Expand All @@ -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)
{
Expand Down Expand Up @@ -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}";
}
}

Expand Down Expand Up @@ -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);
}
Expand All @@ -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)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GetUserByPolicy [](start = 25, length = 15)

rename: GetAccountByPolicy

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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;
Expand Down
4 changes: 2 additions & 2 deletions active-directory-b2c-dotnet-uwp/project.json
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": {}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

10586 [](start = 13, length = 5)

is this needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's not...removed

},
"runtimes": {
"win10-arm": {},
Expand Down