예제 #1
0
                public override async ValueTask <Azure.Core.AccessToken> GetTokenAsync(Azure.Core.TokenRequestContext requestContext, CancellationToken cancellationToken)
                {
                    try
                    {
                        var result = await _tokenProvider.GetAuthenticationResultAsync(_resourceIdentity, _tenantIdentity, cancellationToken : cancellationToken).ConfigureAwait(false);

                        return(new Azure.Core.AccessToken(result.AccessToken, result.ExpiresOn));
                    }
                    catch (Exception ex)
                    {
                        InternalLogger.Error(ex, "AzureEventHub - Failed getting AccessToken from AzureServiceTokenProvider for resource {0}", _resourceIdentity);
                        throw;
                    }
                }
예제 #2
0
        /// <summary>
        /// Acquires an access token for SQL using AzureServiceTokenProvider with the given SQL authentication parameters.
        /// </summary>
        /// <param name="parameters">The parameters needed in order to obtain a SQL access token</param>
        /// <returns></returns>
        public async Task <SqlAuthenticationToken> AcquireTokenAsync(SqlAppAuthenticationParameters parameters)
        {
            string azureAdInstance = UriHelper.GetAzureAdInstanceByAuthority(parameters.Authority);
            string tenantId        = UriHelper.GetTenantByAuthority(parameters.Authority);

            if (string.IsNullOrEmpty(azureAdInstance))
            {
                throw new ArgumentException("The Azure AD instance could not be parsed from the authority provided in SqlAuthenticationParameters");
            }

            if (string.IsNullOrEmpty(parameters.Resource))
            {
                throw new ArgumentException("A resource must be specified in SqlAuthenticationParameters");
            }

            AzureServiceTokenProvider tokenProvider = new AzureServiceTokenProvider(azureAdInstance: azureAdInstance);

            var authResult = await tokenProvider.GetAuthenticationResultAsync(parameters.Resource, tenantId).ConfigureAwait(false);

            return(new SqlAuthenticationToken(authResult.AccessToken, authResult.ExpiresOn));
        }