/// <inheritdoc /> protected override async Task <CacheExecutorResults> InternalExecuteAsync(CommandLineOptions options) { var v1App = PreRegisteredApps.CommonCacheTestV1; string resource = PreRegisteredApps.MsGraph; string[] scopes = new[] { resource + "/user.read" }; CommonCacheTestUtils.EnsureCacheFileDirectoryExists(); var tokenCache = FileBasedTokenCacheHelper.GetUserCache( CommonCacheTestUtils.MsalV2CacheFilePath, CommonCacheTestUtils.AdalV3CacheFilePath); var app = new PublicClientApplication(v1App.ClientId, v1App.Authority, tokenCache); IEnumerable <IAccount> accounts = await app.GetAccountsAsync().ConfigureAwait(false); try { var result = await app.AcquireTokenSilentAsync(scopes, accounts.FirstOrDefault(), app.Authority, false).ConfigureAwait(false); Console.WriteLine($"got token for '{result.Account.Username}' from the cache"); return(new CacheExecutorResults(result.Account.Username, true)); } catch (MsalUiRequiredException) { var result = await app.AcquireTokenByUsernamePasswordAsync(scopes, options.Username, options.UserPassword.ToSecureString()).ConfigureAwait(false); Console.WriteLine($"got token for '{result.Account.Username}' without the cache"); return(new CacheExecutorResults(result.Account.Username, false)); } }
/// <inheritdoc /> protected override async Task <CacheExecutorResults> InternalExecuteAsync(CommandLineOptions options) { var v1App = PreRegisteredApps.CommonCacheTestV1; string resource = PreRegisteredApps.MsGraph; string[] scopes = new[] { resource + "/user.read" }; CommonCacheTestUtils.EnsureCacheFileDirectoryExists(); var app = PublicClientApplicationBuilder .Create(v1App.ClientId) .WithAuthority(new Uri(v1App.Authority), true) .WithLogging((LogLevel level, string message, bool containsPii) => { Console.WriteLine("{0}: {1}", level, message); }) .Build(); FileBasedTokenCacheHelper.ConfigureUserCache( options.CacheStorageType, app.UserTokenCache, CommonCacheTestUtils.AdalV3CacheFilePath, CommonCacheTestUtils.MsalV2CacheFilePath, CommonCacheTestUtils.MsalV3CacheFilePath); IEnumerable <IAccount> accounts = await app.GetAccountsAsync().ConfigureAwait(false); try { var result = await app .AcquireTokenSilent(scopes, accounts.FirstOrDefault()) .WithAuthority(app.Authority) .WithForceRefresh(false) .ExecuteAsync(CancellationToken.None) .ConfigureAwait(false); Console.WriteLine($"got token for '{result.Account.Username}' from the cache"); return(new CacheExecutorResults(result.Account.Username, true)); } catch (MsalUiRequiredException) { var result = await app .AcquireTokenByUsernamePassword(scopes, options.Username, options.UserPassword.ToSecureString()) .ExecuteAsync(CancellationToken.None) .ConfigureAwait(false); Console.WriteLine($"got token for '{result.Account.Username}' without the cache"); return(new CacheExecutorResults(result.Account.Username, false)); } }
/// <inheritdoc /> protected override async Task <IEnumerable <CacheExecutorAccountResult> > InternalExecuteAsync(TestInputData testInputData) { string resource = TestInputData.MsGraph; string[] scopes = new[] { resource + "/user.read" }; var results = new List <CacheExecutorAccountResult>(); foreach (var labUserData in testInputData.LabUserDatas) { var app = PublicClientApplicationBuilder .Create(labUserData.ClientId) .WithAuthority(new Uri(labUserData.Authority), true) .WithLogging((LogLevel level, string message, bool containsPii) => { Console.WriteLine("{0}: {1}", level, message); }) .WithTelemetry(new TraceTelemetryConfig()) .Build(); FileBasedTokenCacheHelper.ConfigureUserCache( testInputData.StorageType, app.UserTokenCache, CommonCacheTestUtils.AdalV3CacheFilePath, CommonCacheTestUtils.MsalV2CacheFilePath, CommonCacheTestUtils.MsalV3CacheFilePath); IEnumerable <IAccount> accounts = await app.GetAccountsAsync().ConfigureAwait(false); IAccount accountToReference = accounts.FirstOrDefault(x => x.Username.Equals(labUserData.Upn, StringComparison.OrdinalIgnoreCase)); try { var result = await app .AcquireTokenSilent(scopes, accountToReference) .WithAuthority(app.Authority) .WithForceRefresh(false) .ExecuteAsync(CancellationToken.None) .ConfigureAwait(false); Console.WriteLine($"got token for '{result.Account.Username}' from the cache"); results.Add(new CacheExecutorAccountResult(labUserData.Upn, result.Account.Username, true)); } catch (MsalUiRequiredException) { var result = await app .AcquireTokenByUsernamePassword(scopes, labUserData.Upn, labUserData.Password.ToSecureString()) .ExecuteAsync(CancellationToken.None) .ConfigureAwait(false); if (string.IsNullOrWhiteSpace(result.AccessToken)) { results.Add(new CacheExecutorAccountResult(labUserData.Upn, string.Empty, false)); } else { Console.WriteLine($"got token for '{result.Account.Username}' without the cache"); results.Add(new CacheExecutorAccountResult(labUserData.Upn, result.Account.Username, false)); } } } return(results); }
/// <inheritdoc /> protected override async Task <IEnumerable <CacheExecutorAccountResult> > InternalExecuteAsync(TestInputData testInputData) { var v1App = PreRegisteredApps.CommonCacheTestV1; string resource = PreRegisteredApps.MsGraph; string[] scopes = new[] { resource + "/user.read" }; Logger.LogCallback = (LogLevel level, string message, bool containsPii) => { Console.WriteLine("{0}: {1}", level, message); }; var tokenCache = new TokenCache(); FileBasedTokenCacheHelper.ConfigureUserCache( testInputData.StorageType, tokenCache, CommonCacheTestUtils.AdalV3CacheFilePath, CommonCacheTestUtils.MsalV2CacheFilePath); var app = new PublicClientApplication(v1App.ClientId, v1App.Authority, tokenCache) { ValidateAuthority = true }; IEnumerable <IAccount> accounts = await app.GetAccountsAsync().ConfigureAwait(false); var results = new List <CacheExecutorAccountResult>(); foreach (var labUserData in testInputData.LabUserDatas) { IAccount accountToReference = accounts.FirstOrDefault(x => x.Username.Equals(labUserData.Upn, StringComparison.OrdinalIgnoreCase)); try { var result = await app.AcquireTokenSilentAsync( scopes, accountToReference, app.Authority, false).ConfigureAwait(false); Console.WriteLine($"got token for '{result.Account.Username}' from the cache"); results.Add(new CacheExecutorAccountResult(labUserData.Upn, result.Account.Username, true)); } catch (MsalUiRequiredException) { var result = await app.AcquireTokenByUsernamePasswordAsync( scopes, labUserData.Upn, labUserData.Password.ToSecureString()).ConfigureAwait(false); if (string.IsNullOrWhiteSpace(result.AccessToken)) { results.Add(new CacheExecutorAccountResult(labUserData.Upn, string.Empty, false)); } else { Console.WriteLine($"got token for '{result.Account.Username}' without the cache"); results.Add(new CacheExecutorAccountResult(labUserData.Upn, result.Account.Username, false)); } } } return(results); }