public async static Task <GraphAPI.Authentication.ResponseObjects.BaseObjects.AzureUserAuth> GetAccessToken(GraphAPI.AuthenticationRequest authenticationRequest, string grant_type, string usr, string pwd)
        {
            string tokenEndpointUri = "https://login.microsoftonline.com/" + authenticationRequest.Credentials.TenantId + "/oauth2/token";// +  // + " / "; + "oauth2/token";

            var content = new FormUrlEncodedContent(new[]
            {
                new KeyValuePair <string, string>("grant_type", grant_type),
                new KeyValuePair <string, string>("username", usr),
                new KeyValuePair <string, string>("password", pwd),

                new KeyValuePair <string, string>("client_id", authenticationRequest.Credentials.ClientId),
                new KeyValuePair <string, string>("client_secret", authenticationRequest.Credentials.ClientSecret),
                new KeyValuePair <string, string>("resource", "https://graph.microsoft.com"),
                new KeyValuePair <string, string>("scope", "https://graph.microsoft.com/.default")
            }
                                                    );

            using (var client = new HttpClient())
            {
                HttpResponseMessage res = await client.PostAsync(tokenEndpointUri, content).ConfigureAwait(false);

                string json = await res.Content.ReadAsStringAsync().ConfigureAwait(false);

                GraphAPI.Authentication.ResponseObjects.BaseObjects.AzureUserAuth aua = JsonConvert.DeserializeObject <GraphAPI.Authentication.ResponseObjects.BaseObjects.AzureUserAuth>(json);

                return(aua);
            }
        }
        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);
            }
        }