/// <summary>
 /// Initializes a new instance of the <see cref="TeamGoalController"/> class.
 /// </summary>
 /// <param name="confidentialClientApp">Instance of ConfidentialClientApplication class.</param>
 /// <param name="logger">Instance to send logs to the Application Insights service.</param>
 /// <param name="azureAdOptions">Instance of IOptions to read data from application configuration.</param>
 /// <param name="teamGoalStorageProvider">Storage provider for working with team goal data in Microsoft Azure Table storage</param>
 /// <param name="tokenAcquisitionHelper">Instance of token acquisition helper to access token.</param>
 public TeamGoalController(
     IConfidentialClientApplication confidentialClientApp,
     ILogger <TeamGoalController> logger,
     IOptions <AzureAdOptions> azureAdOptions,
     ITeamGoalStorageProvider teamGoalStorageProvider,
     TokenAcquisitionHelper tokenAcquisitionHelper)
     : base(confidentialClientApp, azureAdOptions, logger, tokenAcquisitionHelper)
 {
     this.logger = logger;
     this.teamGoalStorageProvider = teamGoalStorageProvider;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="BaseGoalTrackerController"/> class.
 /// </summary>
 /// <param name="confidentialClientApp">Instance of ConfidentialClientApplication class.</param>
 /// <param name="azureAdOptions">Instance of IOptions to read data from application configuration.</param>
 /// <param name="logger">Instance to send logs to the Application Insights service.</param>
 /// <param name="tokenAcquisitionHelper">Instance of token acquisition helper to access token.</param>
 public BaseGoalTrackerController(
     IConfidentialClientApplication confidentialClientApp,
     IOptions <AzureAdOptions> azureAdOptions,
     ILogger logger,
     TokenAcquisitionHelper tokenAcquisitionHelper)
 {
     this.confidentialClientApp = confidentialClientApp;
     this.azureAdOptions        = azureAdOptions;
     this.logger = logger;
     this.tokenAcquisitionHelper = tokenAcquisitionHelper;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="AccessTokenHelper"/> class.
 /// </summary>
 /// <param name="azureAdOptions">Instance of IOptions to read data from application configuration.</param>
 /// <param name="botSettings">Instance of IOptions to read data tenant details.</param>
 /// <param name="confidentialClientApp">Instance of ConfidentialClientApplication class.</param>
 /// <param name="tokenAcquisitionHelper">Instance of token acquisition helper to access token.</param>
 /// <param name="logger">Instance to send logs to the Application Insights service.</param>
 public AccessTokenHelper(
     IOptions <AzureActiveDirectorySettings> azureAdOptions,
     IOptions <BotSettings> botSettings,
     IConfidentialClientApplication confidentialClientApp,
     TokenAcquisitionHelper tokenAcquisitionHelper,
     ILogger <AccessTokenHelper> logger)
 {
     this.azureAdOptions        = azureAdOptions;
     this.botSettings           = botSettings;
     this.confidentialClientApp = confidentialClientApp;
     this.logger = logger;
     this.tokenAcquisitionHelper = tokenAcquisitionHelper;
 }
Beispiel #4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="PersonalGoalNoteController"/> class.
 /// </summary>
 /// <param name="confidentialClientApp">Instance of ConfidentialClientApplication class.</param>
 /// <param name="logger">Instance to send logs to the Application Insights service.</param>
 /// <param name="azureAdOptions">Instance of IOptions to read data from application configuration.</param>
 /// <param name="personalGoalNoteStorageProvider">Storage provider for working with team goal data in Microsoft Azure Table storage</param>
 /// <param name="tokenAcquisitionHelper">Instance of token acquisition helper to access token.</param>
 /// <param name="cardHelper">Instance of class that handles card create/update helper methods.</param>
 /// <param name="backgroundTaskWrapper">Instance of backgroundTaskWrapper to run a background task.</param>
 public PersonalGoalNoteController(
     IConfidentialClientApplication confidentialClientApp,
     ILogger <PersonalGoalNoteController> logger,
     IOptions <AzureAdOptions> azureAdOptions,
     IPersonalGoalNoteStorageProvider personalGoalNoteStorageProvider,
     TokenAcquisitionHelper tokenAcquisitionHelper,
     CardHelper cardHelper,
     BackgroundTaskWrapper backgroundTaskWrapper)
     : base(confidentialClientApp, azureAdOptions, logger, tokenAcquisitionHelper)
 {
     this.logger = logger;
     this.personalGoalNoteStorageProvider = personalGoalNoteStorageProvider;
     this.cardHelper            = cardHelper;
     this.backgroundTaskWrapper = backgroundTaskWrapper;
 }
Beispiel #5
0
        /// <summary>
        /// Get user Azure AD access token.
        /// </summary>
        /// <returns>Token to access MS graph.</returns>
        public async Task <string> GetAccessTokenAsync()
        {
            List <string> scopeList = this.azureAdOptions.Value.GraphScope.Split(new char[] { ' ' }, System.StringSplitOptions.RemoveEmptyEntries).ToList();

            try
            {
                // Gets user account from the accounts available in token cache.
                // https://docs.microsoft.com/en-us/dotnet/api/microsoft.identity.client.clientapplicationbase.getaccountasync?view=azure-dotnet
                // Concatenation of UserObjectId and TenantId separated by a dot is used as unique identifier for getting user account.
                // https://docs.microsoft.com/en-us/dotnet/api/microsoft.identity.client.accountid.identifier?view=azure-dotnet#Microsoft_Identity_Client_AccountId_Identifier
                var account = await this.confidentialClientApp.GetAccountAsync($"{this.UserObjectId}.{this.azureAdOptions.Value.TenantId}");

                // Attempts to acquire an access token for the account from the user token cache.
                // https://docs.microsoft.com/en-us/dotnet/api/microsoft.identity.client.clientapplicationbase.acquiretokensilent?view=azure-dotnet
                AuthenticationResult result = await this.confidentialClientApp
                                              .AcquireTokenSilent(scopeList, account)
                                              .ExecuteAsync();

                return(result.AccessToken);
            }
            catch (MsalUiRequiredException msalex)
            {
                // Getting new token using AddTokenToCacheFromJwtAsync as AcquireTokenSilent failed to load token from cache.
                TokenAcquisitionHelper tokenAcquisitionHelper = new TokenAcquisitionHelper(this.confidentialClientApp);
                try
                {
                    this.logger.LogInformation($"MSAL exception occurred while trying to acquire new token. MSAL exception details are found {msalex}.");
                    var jwtToken = AuthenticationHeaderValue.Parse(this.Request.Headers["Authorization"].ToString()).Parameter;
                    return(await tokenAcquisitionHelper.AddTokenToCacheFromJwtAsync(this.azureAdOptions.Value.GraphScope, jwtToken));
                }
                catch (Exception ex)
                {
                    this.logger.LogError(ex, $"An error occurred in GetAccessTokenAsync: {ex.Message}.");
                    throw;
                }
            }
            catch (Exception ex)
            {
                this.logger.LogError(ex, $"An error occurred in fetching token : {ex.Message}.");
                throw;
            }
        }