public async Task UseCreateApiWithAad() { var appAuthority = ""; var aadAppId = ""; var aadAppSecret = ""; AzureActiveDirectoryTokenProvider.AuthenticationCallback authCallback = async(audience, authority, state) => { var authContext = new AuthenticationContext(authority); var cc = new ClientCredential(aadAppId, aadAppSecret); var authResult = await authContext.AcquireTokenAsync(audience, cc); return(authResult.AccessToken); }; // Create new client with updated connection string. await using (var scope = await EventHubScope.CreateAsync(1)) { var connectionString = TestUtility.BuildEventHubsConnectionString(scope.EventHubName); var csb = new EventHubsConnectionStringBuilder(connectionString); var ehClient = EventHubClient.CreateWithAzureActiveDirectory(csb.Endpoint, csb.EntityPath, authCallback, appAuthority); // Send one event TestUtility.Log($"Sending one message."); var ehSender = ehClient.CreatePartitionSender("0"); var eventData = new EventData(Encoding.UTF8.GetBytes("Hello EventHub!")); await ehSender.SendAsync(eventData); // Receive event. PartitionReceiver ehReceiver = null; try { TestUtility.Log($"Receiving one message."); ehReceiver = ehClient.CreateReceiver(PartitionReceiver.DefaultConsumerGroupName, "0", EventPosition.FromStart()); var msg = await ehReceiver.ReceiveAsync(1); Assert.True(msg != null, "Failed to receive message."); } finally { await ehReceiver?.CloseAsync(); } } }
static async Task UserInteractiveLoginScenarioAsync() { var ehClient = EventHubClient.CreateWithAzureActiveDirectory( new Uri($"sb://{EventHubNamespace}/"), EventHubName, async(audience, authority, state) => { var app = PublicClientApplicationBuilder.Create(ClientId) .WithRedirectUri(ReplyUrl) .Build(); var authResult = await app.AcquireTokenInteractive(new string[] { $"{audience}/.default" }).ExecuteAsync(); return(authResult.AccessToken); }, $"https://login.windows.net/{TenantId}"); await SendReceiveAsync(ehClient); }
public async Task CreateWithAzureActiveDirectory() { await using var scope = await EventHubScope.CreateAsync(1); #region Snippet:EventHubs_Migrate_T1_CreateWithAzureActiveDirectory #if SNIPPET var fullyQualifiedNamespace = "<< NAMESPACE (likely similar to {your-namespace}.servicebus.windows.net) >>"; var eventHubName = "<< NAME OF THE EVENT HUB >>"; var authority = "<< NAME OF THE AUTHORITY TO ASSOCIATE WITH THE TOKEN >>"; var aadAppId = "<< THE AZURE ACTIVE DIRECTORY APPLICATION ID TO REQUEST A TOKEN FOR >>"; var aadAppSecret = "<< THE AZURE ACTIVE DIRECTORY SECRET TO USE FOR THE TOKEN >>"; #else var fullyQualifiedNamespace = new EventHubsConnectionStringBuilder(TestUtility.EventHubsConnectionString).Endpoint.ToString(); var eventHubName = scope.EventHubName; var authority = ""; // Needed for manual run var aadAppId = ""; // Needed for manual run var aadAppSecret = ""; // Needed for manual run #endif AzureActiveDirectoryTokenProvider.AuthenticationCallback authCallback = async(audience, authority, state) => { var authContext = new AuthenticationContext(authority); var clientCredential = new ClientCredential(aadAppId, aadAppSecret); AuthenticationResult authResult = await authContext.AcquireTokenAsync(audience, clientCredential); return(authResult.AccessToken); }; EventHubClient client = EventHubClient.CreateWithAzureActiveDirectory( new Uri(fullyQualifiedNamespace), eventHubName, authCallback, authority); #endregion client.Close(); }