Beispiel #1
0
        // Gets an access token. First tries to get the access token from the token cache.
        // This app uses a password (secret) to authenticate. Production apps should use a certificate.
        //public async Task<string> GetAppAccessTokenAsync()
        //{
        //    try
        //    {
        //        // For development mode purposes only. Production apps should use a client certificate.

        //        return await GetUserAccessTokenAsync(_tenantId);
        //    }
        //    catch (AdalException ex)
        //    {
        //        throw ex;
        //    }
        //}


        // TODO: Once the new method above has been fully tested, deprecate the cmmented code below:
        // Gets an access token. First tries to get the access token from the token cache.
        // This app uses a password (secret) to authenticate. Production apps should use a certificate.
        public async Task <string> GetAppAccessTokenAsync()
        {
            _tokenCache = new AppTokenCache(_tenantId, _memoryCache);

            AuthenticationContext authContext = new AuthenticationContext($"{ _aadInstance }{ _tenantId }", _tokenCache);

            try
            {
                Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationResult authResult = await authContext.AcquireTokenAsync(
                    _graphResourceId,
                    new Microsoft.IdentityModel.Clients.ActiveDirectory.ClientCredential(_appId, _appSecret)); // For development mode purposes only. Production apps should use a client certificate.

                return(authResult.AccessToken);
            }
            catch (AdalException ex)
            {
                throw ex;
            }
        }