/// <summary>
        /// Gets an access token asynchronously.
        /// </summary>
        /// <returns></returns>
        private async Task <string> GetAccessTokenAsync()
        {
            IConfidentialClientApplication app = MSALAppBuilder.BuildConfidentialClientApplication();

            AuthenticationResult   result;
            IEnumerable <IAccount> accounts = await app.GetAccountsAsync();

            string[] scopes = VeracityIntegrationOptions.VeracityPlatformServiceScopes.Split(' ');

            var accountList = accounts.ToList();
            var account     = accountList.FirstOrDefault();

            try
            {
                // try to get an already cached token
                result = await app.AcquireTokenSilent(scopes, account).ExecuteAsync().ConfigureAwait(false);
            }
            catch (MsalUiRequiredException ex)
            {
                // Cannot find any cache user in memory, you should sign out and login again.
                Debug.WriteLine($"Cannot find any cache user in memory {ex.Message}");
                throw;
            }

            return(result.AccessToken);
        }
예제 #2
0
        /// <summary>
        /// Called when an authorization code is received.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <returns></returns>
        private async Task OnAuthorizationCodeReceivedAsync(AuthorizationCodeReceivedNotification context)
        {
            var claimsPrincipal = new ClaimsPrincipal(context.AuthenticationTicket.Identity);

            // Upon successful sign in, get the access token & cache it using MSAL
            IConfidentialClientApplication clientApp = MSALAppBuilder.BuildConfidentialClientApplication(claimsPrincipal);

            AuthenticationResult result = await clientApp
                                          .AcquireTokenByAuthorizationCode(VeracityIntegrationOptions.DefaultScope.Split(' '), context.Code)
                                          .ExecuteAsync();
        }