private async Task <AuthenticationResult> RunTestForUserAsync(LabResponse labResponse, string authority) { var cert = await s_secretProvider.GetCertificateWithPrivateMaterialAsync( CertificateName, KeyVaultInstance.MsalTeam).ConfigureAwait(false); IConfidentialClientApplication cca; string redirectUri = SeleniumWebUI.FindFreeLocalhostRedirectUri(); cca = ConfidentialClientApplicationBuilder .Create(ConfidentialClientID) .WithAuthority(authority) .WithCertificate(cert) .WithRedirectUri(redirectUri) .WithTestLogging() .Build(); Trace.WriteLine("Part 1 - Call GetAuthorizationRequestUrl to figure out where to go "); var startUri = await cca .GetAuthorizationRequestUrl(s_scopes) .ExecuteAsync() .ConfigureAwait(false); Trace.WriteLine("Part 2 - Use a browser to login and to capture the authorization code "); var seleniumUi = new SeleniumWebUI((driver) => { Trace.WriteLine("Starting Selenium automation"); driver.PerformLogin(labResponse.User, Prompt.SelectAccount, false, false); }, TestContext); CancellationTokenSource cts = new CancellationTokenSource(s_timeout); Uri authCodeUri = await seleniumUi.AcquireAuthorizationCodeAsync( startUri, new Uri(redirectUri), cts.Token) .ConfigureAwait(false); var authorizationResult = AuthorizationResult.FromUri(authCodeUri.AbsoluteUri); Assert.AreEqual(AuthorizationStatus.Success, authorizationResult.Status); Trace.WriteLine("Part 3 - Get a token using the auth code, just like a website"); var result = await cca.AcquireTokenByAuthorizationCode(s_scopes, authorizationResult.Code) .ExecuteAsync() .ConfigureAwait(false); return(result); }
private async Task RunTestForUserAsync(LabResponse labResponse, string authority, bool usePkce = false) { var cert = await s_secretProvider.GetCertificateWithPrivateMaterialAsync( CertificateName, KeyVaultInstance.MsalTeam).ConfigureAwait(false); IConfidentialClientApplication cca; string redirectUri = SeleniumWebUI.FindFreeLocalhostRedirectUri(); HttpSnifferClientFactory factory; cca = ConfidentialClientApplicationBuilder .Create(ConfidentialClientID) .WithAuthority(authority) .WithCertificate(cert) .WithRedirectUri(redirectUri) .WithTestLogging(out factory) .Build(); var cacheAccess = (cca as ConfidentialClientApplication).UserTokenCache.RecordAccess(); Trace.WriteLine("Part 1 - Call GetAuthorizationRequestUrl to figure out where to go "); var authUriBuilder = cca .GetAuthorizationRequestUrl(s_scopes); string codeVerifier = ""; if (usePkce) { authUriBuilder.WithPkce(out codeVerifier); } Uri authUri = await authUriBuilder.ExecuteAsync() .ConfigureAwait(false); cacheAccess.AssertAccessCounts(0, 0); Trace.WriteLine("Part 2 - Use a browser to login and to capture the authorization code "); var seleniumUi = new SeleniumWebUI((driver) => { Trace.WriteLine("Starting Selenium automation"); driver.PerformLogin(labResponse.User, Prompt.SelectAccount, false, false); }, TestContext); CancellationTokenSource cts = new CancellationTokenSource(s_timeout); Uri authCodeUri = await seleniumUi.AcquireAuthorizationCodeAsync( authUri, new Uri(redirectUri), cts.Token) .ConfigureAwait(false); var authorizationResult = AuthorizationResult.FromUri(authCodeUri.AbsoluteUri); Assert.AreEqual(AuthorizationStatus.Success, authorizationResult.Status); factory.RequestsAndResponses.Clear(); Trace.WriteLine("Part 3 - Get a token using the auth code, just like a website"); var result = await cca.AcquireTokenByAuthorizationCode(s_scopes, authorizationResult.Code) .WithPkceCodeVerifier(codeVerifier) .WithExtraHttpHeaders(TestConstants.ExtraHttpHeader) .ExecuteAsync() .ConfigureAwait(false); cacheAccess.AssertAccessCounts(0, 1); AssertCacheKey(cacheAccess, result.Account.HomeAccountId.Identifier); AssertExtraHTTPHeadersAreSent(factory); Trace.WriteLine("Part 4 - Remove Account"); await cca.RemoveAsync(result.Account).ConfigureAwait(false); cacheAccess.AssertAccessCounts(0, 2); AssertCacheKey(cacheAccess, result.Account.HomeAccountId.Identifier); }