azureazure-active-directoryazure-eventhubazure-identitydefaultazurecredential

Creating Evenhub topic with EventHubManagementClient


I'm trying to create an EventHub topic within a namespace through my .net core application. I'm a bit struggling with the authentication and couldn't figure out exactly what should I do to get the Token credentials from Azure.

Should I use ConfidentialClientApplicationBuilder? and if yes how exactly I give access to the specific EventHub namespace and get all the data required (clientId, clientSecret, Authorithy and TenantId)?

Thanks ahead.


Solution

  • Given that the legacy package is deprecated and not recommended, this is based on use of the current generation management package, Azure.ResourceManager.EventHubs.

    There is no need to work with Microsoft.Identity.Client or any of the related types; the auth story for the current generation has been simplified to work directly with Azure.Identity token types that manage token acquisition implicitly.

    There's a full sample here, but the important parts are:

    // Create your client
    ArmClient armClient = new ArmClient(new DefaultAzureCredential());
    
    // Get the subscription
    SubscriptionResource subscription = await armClient.GetDefaultSubscriptionAsync();
    
    // Get the resource group
    ResourceGroupResource resourceGroup = await subscription.GetResourceGroupAsync("myRg");
    
    // Get your Event Hubs namespace
    EventHubsNamespaceCollection namespaceCollection = resourceGroup.GetEventHubsNamespaces();
    EventHubsNamespaceResource eventHubNamespace = await namespaceCollection.GetAsync("myNamespace");
    
    // Create your Event Hub
    EventHubResource eventHub = 
        (await eventHubCollection.CreateOrUpdateAsync(
            WaitUntil.Completed, 
            "myEventhub", 
            new EventHubData())
        ).Value;
    

    More information on using Azure.Identity to authenticate can be found in the Overview docs.