public async static Task <GraphAPI.AuthenticationResponse> AuthenticateOnBehalfOf(GraphAPI.AuthenticationRequest authenticationRequest, string authToken)
        {
            int errPoint = 0;

            IntegrationException.ExceptionType exceptionType = IntegrationException.ExceptionType.Internal;
            string requestString  = string.Empty;
            string responseString = string.Empty;
            Dictionary <string, string> requestHeaders = new Dictionary <string, string>();

            try
            {
                // initialise Daemon client by specifying Client Id, Client Secret, Tenant Id, Graph Scope and Authority Format
                IConfidentialClientApplication daemonClient;

                daemonClient = ConfidentialClientApplicationBuilder.Create(authenticationRequest.Credentials.ClientId)
                               .WithAuthority(string.Format(authenticationRequest.AuthorityFormat, authenticationRequest.Credentials.TenantId))
                               .WithRedirectUri(string.Empty)
                               .WithClientSecret(authenticationRequest.Credentials.ClientSecret)
                               .Build();


                UserAssertion ua = new UserAssertion(authToken, "urn:ietf:params:oauth:grant-type:jwt-bearer");

                List <string> scopes = new List <string>();
                scopes.Add(authenticationRequest.GraphScope);

                // .Result to make sure that the cache is filled-in before the controller tries to get access tokens
                var result = daemonClient.AcquireTokenOnBehalfOf(scopes, ua)
                             .ExecuteAsync()
                             .GetAwaiter().GetResult();

                // attempt to retrieve a valid access token to invoke the Graph API operations
                AuthenticationResult authenticationResult = await daemonClient.AcquireTokenForClient(new[] { authenticationRequest.GraphScope })
                                                            .ExecuteAsync().ConfigureAwait(false);

                GraphAPI.AuthenticationResponse authenticationResponse = new GraphAPI.AuthenticationResponse(authenticationResult);

                return(authenticationResponse);
            }
            catch (Exception e)
            {
                string exceptionMessage = e.Message + Environment.NewLine;

                if (e.InnerException != null)
                {
                    exceptionMessage += " Inner Exception - " + e.InnerException + Environment.NewLine;
                }
                throw new IntegrationException(exceptionMessage, "PostData", "Authenticate", exceptionType, errPoint, requestString, requestHeaders, responseString);
            }
        }
        public async static Task <AuthenticationResponse> Authenticate(AuthenticationRequest authenticationRequest)
        {
            int errPoint = 0;

            IntegrationException.ExceptionType exceptionType = IntegrationException.ExceptionType.Internal;
            string requestString  = string.Empty;
            string responseString = string.Empty;
            Dictionary <string, string> requestHeaders = new Dictionary <string, string>();

            try
            {
                // initialise Daemon client by specifying Client Id, Client Secret, Tenant Id, Graph Scope and Authority Format
                IConfidentialClientApplication daemonClient;

                errPoint     = 1;
                daemonClient = ConfidentialClientApplicationBuilder.Create(authenticationRequest.Credentials.ClientId)
                               .WithAuthority(string.Format(authenticationRequest.AuthorityFormat, authenticationRequest.Credentials.TenantId))
                               .WithRedirectUri(string.Empty)
                               .WithClientSecret(authenticationRequest.Credentials.ClientSecret)
                               .Build();

                errPoint = 2;
                // attempt to retrieve a valid access token to invoke the Graph API operations
                AuthenticationResult authenticationResult = await daemonClient.AcquireTokenForClient(new[] { authenticationRequest.GraphScope })
                                                            .ExecuteAsync().ConfigureAwait(false);

                errPoint = 3;
                GraphAPI.AuthenticationResponse authenticationResponse = new GraphAPI.AuthenticationResponse(authenticationResult);

                return(authenticationResponse);
            }
            catch (Exception e)
            {
                string exceptionMessage = e.Message + Environment.NewLine;

                if (e.InnerException != null)
                {
                    exceptionMessage += " Inner Exception - " + e.InnerException + Environment.NewLine;
                }

                throw new Exception("Authenticate[" + errPoint + "] - " + exceptionMessage);
                // throw new IntegrationException(exceptionMessage, "PostData", "Authenticate", exceptionType, errPoint, requestString, requestHeaders, responseString);
            }
        }