private async Task InitializeGlobalProviderAsync() { if (ProviderManager.Instance.GlobalProvider == null) { var provider = new MsalProvider(ClientId, Scopes, null, false, true); // Configure the token cache storage for non-UWP applications. var storageProperties = new StorageCreationPropertiesBuilder(CacheConfig.CacheFileName, CacheConfig.CacheDir) .WithLinuxKeyring( CacheConfig.LinuxKeyRingSchema, CacheConfig.LinuxKeyRingCollection, CacheConfig.LinuxKeyRingLabel, CacheConfig.LinuxKeyRingAttr1, CacheConfig.LinuxKeyRingAttr2) .WithMacKeyChain( CacheConfig.KeyChainServiceName, CacheConfig.KeyChainAccountName) .Build(); await provider.InitTokenCacheAsync(storageProperties); ProviderManager.Instance.GlobalProvider = provider; await provider.TrySilentSignInAsync(); } }
/// <summary> /// Helper function to initialize the token cache for non-UWP apps. MSAL handles this automatically on UWP. /// </summary> /// <param name="provider">The instance of <see cref="MsalProvider"/> to init the cache for.</param> /// <param name="storageProperties">Properties for configuring the storage cache.</param> /// <param name="logger">Passing null uses the default TraceSource logger.</param> /// <returns>A <see cref="Task"/> representing the result of the asynchronous operation.</returns> public static async Task InitTokenCacheAsync( this MsalProvider provider, StorageCreationProperties storageProperties, TraceSource logger = null) { #if !WINDOWS_UWP // Token cache persistence (not required on UWP as MSAL does it for you) var cacheHelper = await MsalCacheHelper.CreateAsync(storageProperties, logger); cacheHelper.RegisterCache(provider.Client.UserTokenCache); #endif }
/// <summary> /// Easily creates a <see cref="MsalProvider"/> from a ClientId. /// </summary> /// <example> /// <code> /// ProviderManager.Instance.GlobalProvider = await QuickCreate.CreateMsalProviderAsync("MyClientId"); /// </code> /// </example> /// <param name="clientid">Registered ClientId</param> /// <param name="redirectUri">RedirectUri for auth response.</param> /// <param name="scopes">List of Scopes to initially request.</param> /// <returns>New <see cref="MsalProvider"/> reference.</returns> public static async Task <MsalProvider> CreateMsalProviderAsync(string clientid, string redirectUri = "https://login.microsoftonline.com/common/oauth2/nativeclient", string[] scopes = null) { var client = PublicClientApplicationBuilder.Create(clientid) .WithAuthority(AzureCloudInstance.AzurePublic, AadAuthorityAudience.AzureAdAndPersonalMicrosoftAccount) .WithRedirectUri(redirectUri) .WithClientName(ProviderManager.ClientName) .WithClientVersion(Assembly.GetExecutingAssembly().GetName().Version.ToString()) .Build(); if (scopes == null) { scopes = new string[] { string.Empty }; } var provider = new InteractiveAuthenticationProvider(client, scopes); return(await MsalProvider.CreateAsync(client, provider)); }