From 50e167460ec9cd115ce7dcd2df1c999bf8aa1f68 Mon Sep 17 00:00:00 2001 From: SameerK-MSFT <83378772+SameerK-MSFT@users.noreply.github.com> Date: Mon, 13 Feb 2023 12:06:39 -0800 Subject: [PATCH 1/2] Restoring MAUI samples for 4.50 --- MAUI/MauiAppB2C/App.xaml | 14 + MAUI/MauiAppB2C/App.xaml.cs | 13 + MAUI/MauiAppB2C/AppShell.xaml | 14 + MAUI/MauiAppB2C/AppShell.xaml.cs | 11 + MAUI/MauiAppB2C/MSALClient/B2CConstants.cs | 19 + MAUI/MauiAppB2C/MSALClient/PCAWrapperB2C.cs | 117 ++++++ MAUI/MauiAppB2C/MSALClient/PlatformConfig.cs | 37 ++ MAUI/MauiAppB2C/MainPage.xaml | 46 +++ MAUI/MauiAppB2C/MainPage.xaml.cs | 78 ++++ MAUI/MauiAppB2C/MauiB2C.csproj | 55 +++ MAUI/MauiAppB2C/MauiProgram.cs | 20 + .../Platforms/Android/AndroidManifest.xml | 24 ++ .../Platforms/Android/MainActivity.cs | 27 ++ .../Platforms/Android/MainApplication.cs | 17 + .../Platforms/Android/MsalActivity.cs | 27 ++ .../Android/Resources/values/colors.xml | 6 + MAUI/MauiAppB2C/Platforms/Windows/App.xaml | 8 + MAUI/MauiAppB2C/Platforms/Windows/App.xaml.cs | 33 ++ .../Platforms/Windows/Package.appxmanifest | 43 ++ .../MauiAppB2C/Platforms/Windows/app.manifest | 15 + MAUI/MauiAppB2C/Platforms/iOS/AppDelegate.cs | 13 + .../Platforms/iOS/Entitlements.plist | 10 + MAUI/MauiAppB2C/Platforms/iOS/Info.plist | 32 ++ MAUI/MauiAppB2C/Platforms/iOS/Program.cs | 17 + .../MauiAppB2C/Properties/launchSettings.json | 8 + MAUI/MauiAppB2C/Resources/AppIcon/appicon.svg | 4 + .../Resources/AppIcon/appiconfg.svg | 8 + .../Resources/Fonts/OpenSans-Regular.ttf | Bin 0 -> 107152 bytes .../Resources/Fonts/OpenSans-Semibold.ttf | Bin 0 -> 111024 bytes .../Resources/Images/dotnet_bot.svg | 93 +++++ MAUI/MauiAppB2C/Resources/Raw/AboutAssets.txt | 15 + MAUI/MauiAppB2C/Resources/Splash/splash.svg | 8 + MAUI/MauiAppB2C/Resources/Styles/Colors.xaml | 44 ++ MAUI/MauiAppB2C/Resources/Styles/Styles.xaml | 384 ++++++++++++++++++ MAUI/MauiAppBasic/App.xaml | 14 + MAUI/MauiAppBasic/App.xaml.cs | 15 + MAUI/MauiAppBasic/AppShell.xaml | 14 + MAUI/MauiAppBasic/AppShell.xaml.cs | 13 + MAUI/MauiAppBasic/MSALClient/AppConstants.cs | 25 ++ MAUI/MauiAppBasic/MSALClient/PCAWrapper.cs | 134 ++++++ .../MauiAppBasic/MSALClient/PlatformConfig.cs | 37 ++ MAUI/MauiAppBasic/MainPage.xaml | 52 +++ MAUI/MauiAppBasic/MainPage.xaml.cs | 89 ++++ MAUI/MauiAppBasic/MauiAppBasic.csproj | 65 +++ MAUI/MauiAppBasic/MauiProgram.cs | 22 + .../Platforms/Android/AndroidManifest.xml | 24 ++ .../Platforms/Android/MainActivity.cs | 40 ++ .../Platforms/Android/MainApplication.cs | 19 + .../Platforms/Android/MsalActivity.cs | 27 ++ .../Android/Resources/values/colors.xml | 6 + MAUI/MauiAppBasic/Platforms/Windows/App.xaml | 8 + .../Platforms/Windows/App.xaml.cs | 37 ++ .../Platforms/Windows/Package.appxmanifest | 43 ++ .../Platforms/Windows/app.manifest | 15 + .../MauiAppBasic/Platforms/iOS/AppDelegate.cs | 24 ++ .../Platforms/iOS/Entitlements.plist | 10 + MAUI/MauiAppBasic/Platforms/iOS/Info.plist | 32 ++ MAUI/MauiAppBasic/Platforms/iOS/Program.cs | 19 + .../Properties/launchSettings.json | 8 + .../Resources/AppIcon/appicon.svg | 4 + .../Resources/AppIcon/appiconfg.svg | 8 + .../Resources/Fonts/OpenSans-Regular.ttf | Bin 0 -> 107140 bytes .../Resources/Fonts/OpenSans-Semibold.ttf | Bin 0 -> 111024 bytes .../Resources/Images/dotnet_bot.svg | 93 +++++ .../Resources/Raw/AboutAssets.txt | 15 + MAUI/MauiAppBasic/Resources/Splash/splash.svg | 8 + .../MauiAppBasic/Resources/Styles/Colors.xaml | 44 ++ .../MauiAppBasic/Resources/Styles/Styles.xaml | 384 ++++++++++++++++++ MAUI/MauiAppWithBroker/App.xaml | 14 + MAUI/MauiAppWithBroker/App.xaml.cs | 23 ++ MAUI/MauiAppWithBroker/AppShell.xaml | 14 + MAUI/MauiAppWithBroker/AppShell.xaml.cs | 13 + .../MSALClient/AppConstants.cs | 22 + .../MSALClient/PCAWrapper.cs | 122 ++++++ .../MSALClient/PlatformConfig.cs | 37 ++ MAUI/MauiAppWithBroker/MainPage.xaml | 47 +++ MAUI/MauiAppWithBroker/MainPage.xaml.cs | 87 ++++ .../MauiAppWithBroker.csproj | 53 +++ MAUI/MauiAppWithBroker/MauiProgram.cs | 34 ++ .../Platforms/Android/AndroidManifest.xml | 24 ++ .../Platforms/Android/MainActivity.cs | 40 ++ .../Platforms/Android/MainApplication.cs | 19 + .../Android/Resources/values/colors.xml | 6 + .../Platforms/Windows/App.xaml | 8 + .../Platforms/Windows/App.xaml.cs | 40 ++ .../Platforms/Windows/Package.appxmanifest | 43 ++ .../Platforms/Windows/app.manifest | 15 + .../Platforms/iOS/AppDelegate.cs | 45 ++ .../Platforms/iOS/Entitlements.plist | 10 + .../Platforms/iOS/Info.plist | 51 +++ .../Platforms/iOS/Program.cs | 19 + .../Properties/launchSettings.json | 8 + .../Resources/AppIcon/appicon.svg | 4 + .../Resources/AppIcon/appiconfg.svg | 8 + .../Resources/Fonts/OpenSans-Regular.ttf | Bin 0 -> 107140 bytes .../Resources/Fonts/OpenSans-Semibold.ttf | Bin 0 -> 111024 bytes .../Resources/Images/dotnet_bot.svg | 93 +++++ .../Resources/Raw/AboutAssets.txt | 15 + .../Resources/Splash/splash.svg | 8 + .../Resources/Styles/Colors.xaml | 44 ++ .../Resources/Styles/Styles.xaml | 384 ++++++++++++++++++ MAUI/MauiApps.sln | 43 ++ 102 files changed, 4001 insertions(+) create mode 100644 MAUI/MauiAppB2C/App.xaml create mode 100644 MAUI/MauiAppB2C/App.xaml.cs create mode 100644 MAUI/MauiAppB2C/AppShell.xaml create mode 100644 MAUI/MauiAppB2C/AppShell.xaml.cs create mode 100644 MAUI/MauiAppB2C/MSALClient/B2CConstants.cs create mode 100644 MAUI/MauiAppB2C/MSALClient/PCAWrapperB2C.cs create mode 100644 MAUI/MauiAppB2C/MSALClient/PlatformConfig.cs create mode 100644 MAUI/MauiAppB2C/MainPage.xaml create mode 100644 MAUI/MauiAppB2C/MainPage.xaml.cs create mode 100644 MAUI/MauiAppB2C/MauiB2C.csproj create mode 100644 MAUI/MauiAppB2C/MauiProgram.cs create mode 100644 MAUI/MauiAppB2C/Platforms/Android/AndroidManifest.xml create mode 100644 MAUI/MauiAppB2C/Platforms/Android/MainActivity.cs create mode 100644 MAUI/MauiAppB2C/Platforms/Android/MainApplication.cs create mode 100644 MAUI/MauiAppB2C/Platforms/Android/MsalActivity.cs create mode 100644 MAUI/MauiAppB2C/Platforms/Android/Resources/values/colors.xml create mode 100644 MAUI/MauiAppB2C/Platforms/Windows/App.xaml create mode 100644 MAUI/MauiAppB2C/Platforms/Windows/App.xaml.cs create mode 100644 MAUI/MauiAppB2C/Platforms/Windows/Package.appxmanifest create mode 100644 MAUI/MauiAppB2C/Platforms/Windows/app.manifest create mode 100644 MAUI/MauiAppB2C/Platforms/iOS/AppDelegate.cs create mode 100644 MAUI/MauiAppB2C/Platforms/iOS/Entitlements.plist create mode 100644 MAUI/MauiAppB2C/Platforms/iOS/Info.plist create mode 100644 MAUI/MauiAppB2C/Platforms/iOS/Program.cs create mode 100644 MAUI/MauiAppB2C/Properties/launchSettings.json create mode 100644 MAUI/MauiAppB2C/Resources/AppIcon/appicon.svg create mode 100644 MAUI/MauiAppB2C/Resources/AppIcon/appiconfg.svg create mode 100644 MAUI/MauiAppB2C/Resources/Fonts/OpenSans-Regular.ttf create mode 100644 MAUI/MauiAppB2C/Resources/Fonts/OpenSans-Semibold.ttf create mode 100644 MAUI/MauiAppB2C/Resources/Images/dotnet_bot.svg create mode 100644 MAUI/MauiAppB2C/Resources/Raw/AboutAssets.txt create mode 100644 MAUI/MauiAppB2C/Resources/Splash/splash.svg create mode 100644 MAUI/MauiAppB2C/Resources/Styles/Colors.xaml create mode 100644 MAUI/MauiAppB2C/Resources/Styles/Styles.xaml create mode 100644 MAUI/MauiAppBasic/App.xaml create mode 100644 MAUI/MauiAppBasic/App.xaml.cs create mode 100644 MAUI/MauiAppBasic/AppShell.xaml create mode 100644 MAUI/MauiAppBasic/AppShell.xaml.cs create mode 100644 MAUI/MauiAppBasic/MSALClient/AppConstants.cs create mode 100644 MAUI/MauiAppBasic/MSALClient/PCAWrapper.cs create mode 100644 MAUI/MauiAppBasic/MSALClient/PlatformConfig.cs create mode 100644 MAUI/MauiAppBasic/MainPage.xaml create mode 100644 MAUI/MauiAppBasic/MainPage.xaml.cs create mode 100644 MAUI/MauiAppBasic/MauiAppBasic.csproj create mode 100644 MAUI/MauiAppBasic/MauiProgram.cs create mode 100644 MAUI/MauiAppBasic/Platforms/Android/AndroidManifest.xml create mode 100644 MAUI/MauiAppBasic/Platforms/Android/MainActivity.cs create mode 100644 MAUI/MauiAppBasic/Platforms/Android/MainApplication.cs create mode 100644 MAUI/MauiAppBasic/Platforms/Android/MsalActivity.cs create mode 100644 MAUI/MauiAppBasic/Platforms/Android/Resources/values/colors.xml create mode 100644 MAUI/MauiAppBasic/Platforms/Windows/App.xaml create mode 100644 MAUI/MauiAppBasic/Platforms/Windows/App.xaml.cs create mode 100644 MAUI/MauiAppBasic/Platforms/Windows/Package.appxmanifest create mode 100644 MAUI/MauiAppBasic/Platforms/Windows/app.manifest create mode 100644 MAUI/MauiAppBasic/Platforms/iOS/AppDelegate.cs create mode 100644 MAUI/MauiAppBasic/Platforms/iOS/Entitlements.plist create mode 100644 MAUI/MauiAppBasic/Platforms/iOS/Info.plist create mode 100644 MAUI/MauiAppBasic/Platforms/iOS/Program.cs create mode 100644 MAUI/MauiAppBasic/Properties/launchSettings.json create mode 100644 MAUI/MauiAppBasic/Resources/AppIcon/appicon.svg create mode 100644 MAUI/MauiAppBasic/Resources/AppIcon/appiconfg.svg create mode 100644 MAUI/MauiAppBasic/Resources/Fonts/OpenSans-Regular.ttf create mode 100644 MAUI/MauiAppBasic/Resources/Fonts/OpenSans-Semibold.ttf create mode 100644 MAUI/MauiAppBasic/Resources/Images/dotnet_bot.svg create mode 100644 MAUI/MauiAppBasic/Resources/Raw/AboutAssets.txt create mode 100644 MAUI/MauiAppBasic/Resources/Splash/splash.svg create mode 100644 MAUI/MauiAppBasic/Resources/Styles/Colors.xaml create mode 100644 MAUI/MauiAppBasic/Resources/Styles/Styles.xaml create mode 100644 MAUI/MauiAppWithBroker/App.xaml create mode 100644 MAUI/MauiAppWithBroker/App.xaml.cs create mode 100644 MAUI/MauiAppWithBroker/AppShell.xaml create mode 100644 MAUI/MauiAppWithBroker/AppShell.xaml.cs create mode 100644 MAUI/MauiAppWithBroker/MSALClient/AppConstants.cs create mode 100644 MAUI/MauiAppWithBroker/MSALClient/PCAWrapper.cs create mode 100644 MAUI/MauiAppWithBroker/MSALClient/PlatformConfig.cs create mode 100644 MAUI/MauiAppWithBroker/MainPage.xaml create mode 100644 MAUI/MauiAppWithBroker/MainPage.xaml.cs create mode 100644 MAUI/MauiAppWithBroker/MauiAppWithBroker.csproj create mode 100644 MAUI/MauiAppWithBroker/MauiProgram.cs create mode 100644 MAUI/MauiAppWithBroker/Platforms/Android/AndroidManifest.xml create mode 100644 MAUI/MauiAppWithBroker/Platforms/Android/MainActivity.cs create mode 100644 MAUI/MauiAppWithBroker/Platforms/Android/MainApplication.cs create mode 100644 MAUI/MauiAppWithBroker/Platforms/Android/Resources/values/colors.xml create mode 100644 MAUI/MauiAppWithBroker/Platforms/Windows/App.xaml create mode 100644 MAUI/MauiAppWithBroker/Platforms/Windows/App.xaml.cs create mode 100644 MAUI/MauiAppWithBroker/Platforms/Windows/Package.appxmanifest create mode 100644 MAUI/MauiAppWithBroker/Platforms/Windows/app.manifest create mode 100644 MAUI/MauiAppWithBroker/Platforms/iOS/AppDelegate.cs create mode 100644 MAUI/MauiAppWithBroker/Platforms/iOS/Entitlements.plist create mode 100644 MAUI/MauiAppWithBroker/Platforms/iOS/Info.plist create mode 100644 MAUI/MauiAppWithBroker/Platforms/iOS/Program.cs create mode 100644 MAUI/MauiAppWithBroker/Properties/launchSettings.json create mode 100644 MAUI/MauiAppWithBroker/Resources/AppIcon/appicon.svg create mode 100644 MAUI/MauiAppWithBroker/Resources/AppIcon/appiconfg.svg create mode 100644 MAUI/MauiAppWithBroker/Resources/Fonts/OpenSans-Regular.ttf create mode 100644 MAUI/MauiAppWithBroker/Resources/Fonts/OpenSans-Semibold.ttf create mode 100644 MAUI/MauiAppWithBroker/Resources/Images/dotnet_bot.svg create mode 100644 MAUI/MauiAppWithBroker/Resources/Raw/AboutAssets.txt create mode 100644 MAUI/MauiAppWithBroker/Resources/Splash/splash.svg create mode 100644 MAUI/MauiAppWithBroker/Resources/Styles/Colors.xaml create mode 100644 MAUI/MauiAppWithBroker/Resources/Styles/Styles.xaml create mode 100644 MAUI/MauiApps.sln diff --git a/MAUI/MauiAppB2C/App.xaml b/MAUI/MauiAppB2C/App.xaml new file mode 100644 index 0000000..f82e11f --- /dev/null +++ b/MAUI/MauiAppB2C/App.xaml @@ -0,0 +1,14 @@ + + + + + + + + + + + diff --git a/MAUI/MauiAppB2C/App.xaml.cs b/MAUI/MauiAppB2C/App.xaml.cs new file mode 100644 index 0000000..a5ccd60 --- /dev/null +++ b/MAUI/MauiAppB2C/App.xaml.cs @@ -0,0 +1,13 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. +namespace MauiB2C; + +public partial class App : Application +{ + public App() + { + InitializeComponent(); + + MainPage = new AppShell(); + } +} diff --git a/MAUI/MauiAppB2C/AppShell.xaml b/MAUI/MauiAppB2C/AppShell.xaml new file mode 100644 index 0000000..dd6266e --- /dev/null +++ b/MAUI/MauiAppB2C/AppShell.xaml @@ -0,0 +1,14 @@ + + + + + + diff --git a/MAUI/MauiAppB2C/AppShell.xaml.cs b/MAUI/MauiAppB2C/AppShell.xaml.cs new file mode 100644 index 0000000..1c2e3a5 --- /dev/null +++ b/MAUI/MauiAppB2C/AppShell.xaml.cs @@ -0,0 +1,11 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. +namespace MauiB2C; + +public partial class AppShell : Shell +{ + public AppShell() + { + InitializeComponent(); + } +} diff --git a/MAUI/MauiAppB2C/MSALClient/B2CConstants.cs b/MAUI/MauiAppB2C/MSALClient/B2CConstants.cs new file mode 100644 index 0000000..2c25dce --- /dev/null +++ b/MAUI/MauiAppB2C/MSALClient/B2CConstants.cs @@ -0,0 +1,19 @@ +namespace MauiB2C.MSALClient +{ + public static class B2CConstants + { + // Azure AD B2C Coordinates + public const string Tenant = "fabrikamb2c.onmicrosoft.com"; + public const string AzureADB2CHostname = "fabrikamb2c.b2clogin.com"; + public const string ClientID = "e5737214-6372-472c-a85a-68e8fbe6cf3c"; + public static readonly string RedirectUri = $"msal{ClientID}://auth"; + public const string PolicySignUpSignIn = "b2c_1_susi"; + + public static readonly string[] Scopes = { "https://fabrikamb2c.onmicrosoft.com/helloapi/demo.read" }; + + public static readonly string AuthorityBase = $"https://{AzureADB2CHostname}/tfp/{Tenant}/"; + public static readonly string AuthoritySignInSignUp = $"{AuthorityBase}{PolicySignUpSignIn}"; + + public const string IOSKeyChainGroup = "com.microsoft.adalcache"; + } +} diff --git a/MAUI/MauiAppB2C/MSALClient/PCAWrapperB2C.cs b/MAUI/MauiAppB2C/MSALClient/PCAWrapperB2C.cs new file mode 100644 index 0000000..d2c68b2 --- /dev/null +++ b/MAUI/MauiAppB2C/MSALClient/PCAWrapperB2C.cs @@ -0,0 +1,117 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using System; +using System.Diagnostics; +using Microsoft.Identity.Client; +using Microsoft.IdentityModel.Abstractions; + +namespace MauiB2C.MSALClient +{ + /// + /// This is a wrapper for PublicClientApplication. It is singleton. + /// + public class PCAWrapperB2C + { + /// + /// This is the singleton used by ux. Since PCAWrapper constructor does not have perf or memory issue, it is instantiated directly. + /// + public static PCAWrapperB2C Instance { get; private set; } = new PCAWrapperB2C(); + + /// + /// Instance of PublicClientApplication. It is provided, if App wants more customization. + /// + internal IPublicClientApplication PCA { get; } + + // private constructor for singleton + private PCAWrapperB2C() + { + // Create PCA once. Make sure that all the config parameters below are passed + PCA = PublicClientApplicationBuilder + .Create(B2CConstants.ClientID) + .WithExperimentalFeatures() // this is for upcoming logger + .WithLogging(_logger) + .WithB2CAuthority(B2CConstants.AuthoritySignInSignUp) + .WithIosKeychainSecurityGroup(B2CConstants.IOSKeyChainGroup) + .WithRedirectUri(B2CConstants.RedirectUri) + .Build(); + } + + /// + /// Acquire the token silently + /// + /// desired scopes + /// Authentication result + public async Task AcquireTokenSilentAsync(string[] scopes) + { + // Get accounts by policy + IEnumerable accounts = await PCA.GetAccountsAsync(B2CConstants.PolicySignUpSignIn).ConfigureAwait(false); + + AuthenticationResult authResult = await PCA.AcquireTokenSilent(scopes, accounts.FirstOrDefault()) + .WithB2CAuthority(B2CConstants.AuthoritySignInSignUp) + .ExecuteAsync() + .ConfigureAwait(false); + + return authResult; + } + + /// + /// Perform the interactive acquisition of the token for the given scope + /// + /// desired scopes + /// + internal async Task AcquireTokenInteractiveAsync(string[] scopes) + { + return await PCA.AcquireTokenInteractive(B2CConstants.Scopes) + .WithParentActivityOrWindow(PlatformConfig.Instance.ParentWindow) + .ExecuteAsync() + .ConfigureAwait(false); + } + + /// + /// It will sign out the user. + /// + /// + internal async Task SignOutAsync() + { + var accounts = await PCA.GetAccountsAsync().ConfigureAwait(false); + foreach (var acct in accounts) + { + await PCA.RemoveAsync(acct).ConfigureAwait(false); + } + } + + // Custom logger for sample + private MyLogger _logger = new MyLogger(); + + // Custom logger class + private class MyLogger : IIdentityLogger + { + /// + /// Checks if log is enabled or not based on the Entry level + /// + /// + /// + public bool IsEnabled(EventLogLevel eventLogLevel) + { + //Try to pull the log level from an environment variable + var msalEnvLogLevel = Environment.GetEnvironmentVariable("MSAL_LOG_LEVEL"); + + EventLogLevel envLogLevel = EventLogLevel.Informational; + Enum.TryParse(msalEnvLogLevel, out envLogLevel); + + return envLogLevel <= eventLogLevel; + } + + /// + /// Log to console for demo purpose + /// + /// Log Entry values + public void Log(LogEntry entry) + { + Console.WriteLine(entry.Message); + } + } + + } +} diff --git a/MAUI/MauiAppB2C/MSALClient/PlatformConfig.cs b/MAUI/MauiAppB2C/MSALClient/PlatformConfig.cs new file mode 100644 index 0000000..395179e --- /dev/null +++ b/MAUI/MauiAppB2C/MSALClient/PlatformConfig.cs @@ -0,0 +1,37 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MauiB2C.MSALClient +{ + /// + /// Platform specific configuration. + /// + public class PlatformConfig + { + /// + /// Instance to store data + /// + public static PlatformConfig Instance { get; } = new PlatformConfig(); + + /// + /// Platform specific Redirect URI + /// + public string RedirectUri { get; set; } = $"msal{B2CConstants.ClientID}://auth"; + + /// + /// Platform specific parent window + /// + public object ParentWindow { get; set; } + + // private constructor to ensure singleton + private PlatformConfig() + { + } + } +} diff --git a/MAUI/MauiAppB2C/MainPage.xaml b/MAUI/MauiAppB2C/MainPage.xaml new file mode 100644 index 0000000..a43f328 --- /dev/null +++ b/MAUI/MauiAppB2C/MainPage.xaml @@ -0,0 +1,46 @@ + + + + + + + + +