コード例 #1
0
            /// <inheritdoc />
            protected override async Task <CacheExecutorResults> InternalExecuteAsync(CommandLineOptions options)
            {
                var    app      = PreRegisteredApps.CommonCacheTestV1;
                string resource = PreRegisteredApps.MsGraph;

                CommonCacheTestUtils.EnsureCacheFileDirectoryExists();
                var tokenCache = new FileBasedTokenCache(
                    CommonCacheTestUtils.AdalV3CacheFilePath,
                    CommonCacheTestUtils.MsalV2CacheFilePath);
                var authenticationContext = new AuthenticationContext(app.Authority, tokenCache);

                try
                {
                    var result = await authenticationContext.AcquireTokenSilentAsync(
                        resource,
                        app.ClientId,
                        new UserIdentifier(options.Username, UserIdentifierType.RequiredDisplayableId)).ConfigureAwait(false);

                    Console.WriteLine($"got token for '{result.UserInfo.DisplayableId}' from the cache");
                    return(new CacheExecutorResults(result.UserInfo.DisplayableId, true));
                }
                catch (AdalSilentTokenAcquisitionException)
                {
                    var result = await authenticationContext.AcquireTokenAsync(
                        resource,
                        app.ClientId,
                        new UserPasswordCredential(options.Username, options.UserPassword)).ConfigureAwait(false);

                    Console.WriteLine($"got token for '{result.UserInfo.DisplayableId}' without the cache");
                    return(new CacheExecutorResults(result.UserInfo.DisplayableId, false));
                }
            }
            /// <inheritdoc />
            protected override async Task <IEnumerable <CacheExecutorAccountResult> > InternalExecuteAsync(TestInputData testInputData)
            {
                var    app      = PreRegisteredApps.CommonCacheTestV1;
                string resource = PreRegisteredApps.MsGraph;

                LoggerCallbackHandler.LogCallback = (LogLevel level, string message, bool containsPii) =>
                {
                    Console.WriteLine("{0}: {1}", level, message);
                };

                var tokenCache = new FileBasedTokenCache(
                    testInputData.StorageType,
                    CommonCacheTestUtils.AdalV3CacheFilePath,
                    CommonCacheTestUtils.MsalV2CacheFilePath);
                var authenticationContext = new AuthenticationContext(app.Authority, tokenCache);

                var results = new List <CacheExecutorAccountResult>();

                foreach (var labUserData in testInputData.LabUserDatas)
                {
                    try
                    {
                        var result = await authenticationContext.AcquireTokenSilentAsync(
                            resource,
                            app.ClientId,
                            new UserIdentifier(labUserData.Upn, UserIdentifierType.RequiredDisplayableId)).ConfigureAwait(false);

                        Console.WriteLine($"got token for '{result.UserInfo.DisplayableId}' from the cache");
                        results.Add(new CacheExecutorAccountResult(
                                        labUserData.Upn,
                                        result.UserInfo.DisplayableId,
                                        true));
                    }
                    catch (AdalSilentTokenAcquisitionException)
                    {
                        var result = await authenticationContext.AcquireTokenAsync(
                            resource,
                            app.ClientId,
                            new UserPasswordCredential(labUserData.Upn, labUserData.Password)).ConfigureAwait(false);

                        if (string.IsNullOrWhiteSpace(result.AccessToken))
                        {
                            results.Add(new CacheExecutorAccountResult(labUserData.Upn, string.Empty, false));
                        }
                        else
                        {
                            Console.WriteLine($"got token for '{result.UserInfo.DisplayableId}' without the cache");
                            results.Add(new CacheExecutorAccountResult(
                                            labUserData.Upn,
                                            result.UserInfo.DisplayableId,
                                            false));
                        }
                    }
                }

                return(results);
            }