diff --git a/1-Basic/UserDetailsClient/UserDetailsClient.Droid/MainActivity.cs b/1-Basic/UserDetailsClient/UserDetailsClient.Droid/MainActivity.cs index d353737..ef1f9ac 100644 --- a/1-Basic/UserDetailsClient/UserDetailsClient.Droid/MainActivity.cs +++ b/1-Basic/UserDetailsClient/UserDetailsClient.Droid/MainActivity.cs @@ -9,7 +9,8 @@ using Microsoft.Identity.Client; using Android.Content; using Microsoft.Identity.Client.Platforms.Android; - +using Microsoft.Identity.Client.Helper; + namespace UserDetailsClient.Droid { [Activity(Label = "UserDetailsClient", Icon = "@drawable/icon", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)] @@ -21,7 +22,8 @@ protected override void OnCreate(Bundle bundle) global::Xamarin.Forms.Forms.Init(this, bundle); LoadApplication(new App()); - App.ParentWindow = this; + App.ParentWindow = this; + PCAHelper.Instance.ParentWindow = this; } protected override void OnActivityResult(int requestCode, Result resultCode, Intent data) diff --git a/1-Basic/UserDetailsClient/UserDetailsClient.UWP/App.xaml.cs b/1-Basic/UserDetailsClient/UserDetailsClient.UWP/App.xaml.cs index cdf7f01..2ff0d74 100644 --- a/1-Basic/UserDetailsClient/UserDetailsClient.UWP/App.xaml.cs +++ b/1-Basic/UserDetailsClient/UserDetailsClient.UWP/App.xaml.cs @@ -1,4 +1,5 @@ -using System; +using Microsoft.Identity.Client.Helper; +using System; using System.Collections.Generic; using System.IO; using System.Linq; diff --git a/1-Basic/UserDetailsClient/UserDetailsClient.UWP/MainPage.xaml.cs b/1-Basic/UserDetailsClient/UserDetailsClient.UWP/MainPage.xaml.cs index abe8299..e8e832a 100644 --- a/1-Basic/UserDetailsClient/UserDetailsClient.UWP/MainPage.xaml.cs +++ b/1-Basic/UserDetailsClient/UserDetailsClient.UWP/MainPage.xaml.cs @@ -1,4 +1,5 @@ -using System; +using Microsoft.Identity.Client.Helper; +using System; using System.Collections.Generic; using System.IO; using System.Linq; @@ -32,7 +33,8 @@ public MainPage() string redirectUriWithWAM = $"ms-appx-web://microsoft.aad.brokerplugin/{sid}"; // Then use the following: - LoadApplication(new UserDetailsClient.App(redirectURIForSsoWithoutBroker.AbsoluteUri)); + LoadApplication(new UserDetailsClient.App(redirectURIForSsoWithoutBroker.AbsoluteUri)); + PCAHelper.Instance.IsUWP = true; } } } diff --git a/1-Basic/UserDetailsClient/UserDetailsClient/App.cs b/1-Basic/UserDetailsClient/UserDetailsClient/App.cs index 9f47761..3a0758f 100644 --- a/1-Basic/UserDetailsClient/UserDetailsClient/App.cs +++ b/1-Basic/UserDetailsClient/UserDetailsClient/App.cs @@ -1,4 +1,5 @@ using Microsoft.Identity.Client; +using Microsoft.Identity.Client.Helper; using System; using Xamarin.Forms; @@ -6,8 +7,6 @@ namespace UserDetailsClient { public class App : Application { - public static IPublicClientApplication PCA = null; - /// /// The ClientID is the Application ID found in the portal (https://go.microsoft.com/fwlink/?linkid=2083908). /// You can use the below id however if you create an app of your own you should replace the value here. @@ -19,12 +18,11 @@ public class App : Application public static object ParentWindow { get; set; } + public static IPCAHelper PCA { get; private set; } + public App(string specialRedirectUri = null) - { - PCA = PublicClientApplicationBuilder.Create(ClientID) - .WithRedirectUri(specialRedirectUri?? $"msal{ClientID}://auth") - .WithIosKeychainSecurityGroup("com.microsoft.adalcache") - .Build(); + { + PCA = PCAHelper.Init(ClientID, useBroker: false); MainPage = new NavigationPage(new UserDetailsClient.MainPage()); } diff --git a/1-Basic/UserDetailsClient/UserDetailsClient/MainPage.xaml.cs b/1-Basic/UserDetailsClient/UserDetailsClient/MainPage.xaml.cs index 9cb2246..575aced 100644 --- a/1-Basic/UserDetailsClient/UserDetailsClient/MainPage.xaml.cs +++ b/1-Basic/UserDetailsClient/UserDetailsClient/MainPage.xaml.cs @@ -1,4 +1,5 @@ using Microsoft.Identity.Client; +using Microsoft.Identity.Client.Helper; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; @@ -19,68 +20,29 @@ public MainPage() async void OnSignInSignOut(object sender, EventArgs e) { - AuthenticationResult authResult = null; - IEnumerable accounts = await App.PCA.GetAccountsAsync().ConfigureAwait(false); try { if (btnSignInSignOut.Text == "Sign in") { - try + var authResult = await PCAHelper.Instance.AcquireTokenAsync(App.Scopes, customizeInteractive: (builder) => { - IAccount firstAccount = accounts.FirstOrDefault(); - authResult = await App.PCA.AcquireTokenSilent(App.Scopes, firstAccount) - .ExecuteAsync() - .ConfigureAwait(false); - } - catch (MsalUiRequiredException) - { - try - { - var builder = App.PCA.AcquireTokenInteractive(App.Scopes) - .WithParentActivityOrWindow(App.ParentWindow); - - if (Device.RuntimePlatform != "UWP") - { - // on Android and iOS, prefer to use the system browser, which does not exist on UWP - SystemWebViewOptions systemWebViewOptions = new SystemWebViewOptions() - { - iOSHidePrivacyPrompt = true, - }; - - builder.WithSystemWebViewOptions(systemWebViewOptions); - builder.WithUseEmbeddedWebView(false); - } - - authResult = await builder.ExecuteAsync().ConfigureAwait(false); - } - catch (Exception ex2) - { - Device.BeginInvokeOnMainThread(async () => - { - await DisplayAlert("Acquire token interactive failed. See exception message for details: ", ex2.Message, "Dismiss"); - }); - } - } + builder.WithAuthority(AadAuthorityAudience.AzureAdAndPersonalMicrosoftAccount); + }).ConfigureAwait(false); if (authResult != null) { - var content = await GetHttpContentWithTokenAsync(authResult.AccessToken); + var content = await GetHttpContentWithTokenAsync().ConfigureAwait(false); UpdateUserContent(content); } } else { - while (accounts.Any()) - { - await App.PCA.RemoveAsync(accounts.FirstOrDefault()).ConfigureAwait(false); - accounts = await App.PCA.GetAccountsAsync().ConfigureAwait(false); - } + await PCAHelper.Instance.SignOutAsync().ConfigureAwait(false); - - Device.BeginInvokeOnMainThread(() => + Device.BeginInvokeOnMainThread(() => { slUser.IsVisible = false; - btnSignInSignOut.Text = "Sign in"; + btnSignInSignOut.Text = "Sign in"; }); } } @@ -88,14 +50,14 @@ async void OnSignInSignOut(object sender, EventArgs e) { Device.BeginInvokeOnMainThread(async () => { - await DisplayAlert("Authentication failed. See exception message for details: ", ex.Message, "Dismiss"); + await DisplayAlert("Authentication failed. See exception message for details: ", ex.Message, "Dismiss").ConfigureAwait(false); }); } } private void UpdateUserContent(string content) { - if(!string.IsNullOrEmpty(content)) + if (!string.IsNullOrEmpty(content)) { JObject user = JObject.Parse(content); @@ -114,19 +76,19 @@ private void UpdateUserContent(string content) } } - public async Task GetHttpContentWithTokenAsync(string token) + public async Task GetHttpContentWithTokenAsync() { try { //get data from API HttpClient client = new HttpClient(); HttpRequestMessage message = new HttpRequestMessage(HttpMethod.Get, "https://graph.microsoft.com/v1.0/me"); - message.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token); + PCAHelper.Instance.AddAuthenticationBearerToken(message); HttpResponseMessage response = await client.SendAsync(message).ConfigureAwait(false); string responseString = await response.Content.ReadAsStringAsync().ConfigureAwait(false); return responseString; } - catch(Exception ex) + catch (Exception ex) { Device.BeginInvokeOnMainThread(async () => { diff --git a/1-Basic/UserDetailsClient/UserDetailsClient/UserDetailsClient.csproj b/1-Basic/UserDetailsClient/UserDetailsClient/UserDetailsClient.csproj index 0791acc..bbb8218 100644 --- a/1-Basic/UserDetailsClient/UserDetailsClient/UserDetailsClient.csproj +++ b/1-Basic/UserDetailsClient/UserDetailsClient/UserDetailsClient.csproj @@ -15,4 +15,12 @@ + + + Helper\PCAHelper.cs + + + Helper\IPCAHelper.cs + + \ No newline at end of file diff --git a/2-With-broker/UserDetailsClient/UserDetailsClient.Droid/MainActivity.cs b/2-With-broker/UserDetailsClient/UserDetailsClient.Droid/MainActivity.cs index d353737..8e25751 100644 --- a/2-With-broker/UserDetailsClient/UserDetailsClient.Droid/MainActivity.cs +++ b/2-With-broker/UserDetailsClient/UserDetailsClient.Droid/MainActivity.cs @@ -9,7 +9,8 @@ using Microsoft.Identity.Client; using Android.Content; using Microsoft.Identity.Client.Platforms.Android; - +using Microsoft.Identity.Client.Helper; + namespace UserDetailsClient.Droid { [Activity(Label = "UserDetailsClient", Icon = "@drawable/icon", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)] @@ -20,8 +21,8 @@ protected override void OnCreate(Bundle bundle) base.OnCreate(bundle); global::Xamarin.Forms.Forms.Init(this, bundle); - LoadApplication(new App()); - App.ParentWindow = this; + LoadApplication(new App()); + PCAHelper.Instance.ParentWindow = this; } protected override void OnActivityResult(int requestCode, Result resultCode, Intent data) diff --git a/2-With-broker/UserDetailsClient/UserDetailsClient.UWP/App.xaml.cs b/2-With-broker/UserDetailsClient/UserDetailsClient.UWP/App.xaml.cs index cdf7f01..928d17c 100644 --- a/2-With-broker/UserDetailsClient/UserDetailsClient.UWP/App.xaml.cs +++ b/2-With-broker/UserDetailsClient/UserDetailsClient.UWP/App.xaml.cs @@ -1,4 +1,5 @@ -using System; +using Microsoft.Identity.Client.Helper; +using System; using System.Collections.Generic; using System.IO; using System.Linq; @@ -30,6 +31,7 @@ public App() { this.InitializeComponent(); this.Suspending += OnSuspending; + PCAHelper.IsUWP = true; } /// diff --git a/2-With-broker/UserDetailsClient/UserDetailsClient.iOS/AppDelegate.cs b/2-With-broker/UserDetailsClient/UserDetailsClient.iOS/AppDelegate.cs index f6d6ecc..60fda2f 100644 --- a/2-With-broker/UserDetailsClient/UserDetailsClient.iOS/AppDelegate.cs +++ b/2-With-broker/UserDetailsClient/UserDetailsClient.iOS/AppDelegate.cs @@ -6,6 +6,7 @@ using UIKit; using Microsoft.Identity.Client; using Microsoft.Identity.Client.Platforms.iOS; +using Microsoft.Identity.Client.Helper; namespace UserDetailsClient.iOS { @@ -26,7 +27,7 @@ public override bool FinishedLaunching(UIApplication app, NSDictionary options) { global::Xamarin.Forms.Forms.Init(); LoadApplication(new App()); - App.ParentWindow = new UIViewController(); // iOS broker requires a view controller + PCAHelper.Instance.ParentWindow = new UIViewController(); // iOS broker requires a view controller return base.FinishedLaunching(app, options); } diff --git a/2-With-broker/UserDetailsClient/UserDetailsClient/App.cs b/2-With-broker/UserDetailsClient/UserDetailsClient/App.cs index 5539da2..708eded 100644 --- a/2-With-broker/UserDetailsClient/UserDetailsClient/App.cs +++ b/2-With-broker/UserDetailsClient/UserDetailsClient/App.cs @@ -5,8 +5,6 @@ namespace UserDetailsClient { public class App : Application { - public static IPublicClientApplication PCA = null; - /// /// The ClientID is the Application ID found in the portal (https://go.microsoft.com/fwlink/?linkid=2083908). /// You can use the below id however if you create an app of your own you should replace the value here. @@ -25,14 +23,8 @@ public class App : Application public static string[] Scopes = { "User.Read" }; public static string Username = string.Empty; - public static object ParentWindow { get; set; } - public App() { - PCA = PublicClientApplicationBuilder.Create(ClientID) - .WithRedirectUri($"msal{ClientID}://auth") - .Build(); - MainPage = new NavigationPage(new UserDetailsClient.MainPage()); } diff --git a/2-With-broker/UserDetailsClient/UserDetailsClient/MainPage.xaml b/2-With-broker/UserDetailsClient/UserDetailsClient/MainPage.xaml index 0186983..07c75e0 100644 --- a/2-With-broker/UserDetailsClient/UserDetailsClient/MainPage.xaml +++ b/2-With-broker/UserDetailsClient/UserDetailsClient/MainPage.xaml @@ -12,7 +12,7 @@