/// <summary> /// Authenticates the user. /// </summary> /// <returns>Task tracking operation.</returns> public async Task AuthenticateUserAsync() { // If user is already authenticated skip authenticating the user. if (this.tenantBasedTokenMap.Count == 0) { this.logger.LogInformation("Logging the user in with Common tenant info"); UserAuthenticationDetails authDetails = await this.AcquireAzureManagementTokenAsync("common", usePrompt : true).ConfigureAwait(false); this.userIdentifier = authDetails.Username; this.tenantBasedTokenMap[this.GetTenantOnToken(authDetails)] = authDetails; } }
/// <summary> /// For a given token tries to update the token cache. /// </summary> /// <param name="tenantId">Tenant Id to update cache for.</param> /// <returns>Task tracking operation.</returns> private async Task TryUpdateTokenCacheForTenantAsync(string tenantId) { // Optimization to skip refetching tokens. AAD tokens live for 1 hour. if (!this.tenantBasedTokenMap.ContainsKey(tenantId)) { try { this.logger.LogInformation("Get token with '{0}' tenant info", tenantId); UserAuthenticationDetails tenantizedToken = await this.AcquireAzureManagementTokenAsync(tenantId, usePrompt : false, this.userIdentifier).ConfigureAwait(false); this.tenantBasedTokenMap[this.GetTenantOnToken(tenantizedToken)] = tenantizedToken; } catch (Exception ex) { this.logger.LogWarning(ex, $"Failed to acquire token for tenant with Id '{tenantId}'"); } } }