- An Azure Subscription (you can also create a free account)
- A Linux machine (or the great Linux Subsystem on Windows 10)
- A working installation of Kubectl (tutorial here)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static async Task ServiceBusTest() | |
{ | |
// Managed identity needs "Azure Service Bus Data Owner" role assigned and scoped to ServiceBus | |
var tokenProvider = Microsoft.Azure.ServiceBus.Primitives.TokenProvider.CreateManagedIdentityTokenProvider(); | |
TopicClient topicSendClient = new TopicClient($"sb://{sbNamespace}.servicebus.windows.net/", sbEntity, tokenProvider, Microsoft.Azure.ServiceBus.TransportType.Amqp, null); | |
await topicSendClient.SendAsync(new Message(Encoding.UTF8.GetBytes("Hello world from Managed Identity"))); | |
await topicSendClient.CloseAsync(); | |
Console.WriteLine($"Message sent to {sbNamespace}"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static async Task<RestClient> GetRestClientAsync() | |
{ | |
var azureServiceTokenProvider = new AzureServiceTokenProvider(); | |
var accessToken = await azureServiceTokenProvider.GetAccessTokenAsync("https://management.azure.com/").ConfigureAwait(false); | |
var restTokenCredentials = new Microsoft.Rest.TokenCredentials(accessToken); | |
var azCred = new AzureCredentials(restTokenCredentials, null, tenantId, AzureEnvironment.AzureGlobalCloud); | |
return RestClient.Configure().WithEnvironment(AzureEnvironment.AzureGlobalCloud).WithCredentials(azCred).Build(); | |
} | |
static async Task CosmosDBTest() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static async Task StorageTest() | |
{ | |
#region Using management SDK to get the keys. (to be used, for instance, with Event Host Processor) | |
var storManageCredentials = new Microsoft.Azure.Management.Storage.StorageManagementClient(tokenCredentials); | |
storManageCredentials.SubscriptionId = subscriptionId; | |
var accountsas = await storManageCredentials.StorageAccounts.ListKeysWithHttpMessagesAsync(resourceGroupName, storageAccountName); | |
var storCred = new StorageCredentials(resourceGroupName, accountsas.Body.Keys.FirstOrDefault().Value); | |
Console.WriteLine($"Primary key for storage account: {accountsas.Body.Keys.FirstOrDefault().Value}"); | |
#endregion |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//The managed identity needs the Azure Event Hub Data Owner/Sender/Receiver role (depending on what you need to achieve) | |
static async Task GetEventHubClient() | |
{ | |
var ehClient = Microsoft.Azure.EventHubs.EventHubClient.CreateWithManagedIdentity(new Uri($"sb://{ehNamespace}.servicebus.windows.net"), entityPath); | |
await ehClient.SendAsync(new EventData(Encoding.UTF8.GetBytes("Hello from Managed Identity"))); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static async Task GetIoTHubResource() | |
{ | |
var azureServiceTokenProvider = new Microsoft.Azure.Services.AppAuthentication.AzureServiceTokenProvider(); | |
var accessToken = await azureServiceTokenProvider.GetAccessTokenAsync("https://management.azure.com/"); | |
var tokenCredentials = new Microsoft.Rest.TokenCredentials(accessToken); | |
var iotHubClient = new Microsoft.Azure.Management.IotHub.IotHubClient(tokenCredentials); | |
iotHubClient.SubscriptionId = subscriptionId; | |
var iotHubDescription = await iotHubClient.IotHubResource.GetAsync(resourceGroupName,iotHubName); |