In this article, let’s create Azure Queue Storage resources using Azure CLI. Assign “Storage Queue Data Contributor” role to Microsoft Entra (Azure AD) user name allows user account permission to create queues, send and receive messages using Azure RBAC. Build a .NET console application using Azure.Storage.Queues SDK to create and and delete queue as well as to send and receive messages. Sign in to Azure and run the application to verify the result. After this exercise, you will be able to send and receive messages from Azure Queue Storage using .NET Client Library.
Following tasks are performed in this article:
Create Azure Queue Storage resources.
Assign role to Microsoft Entra (Azure AD) username.
Send and receive messages using .NET Client Library.
Sign in to Azure and run application.
Clean up resources.
Create Azure Queue Storage Resources
First basic task is to create and make ready Azure Queue Storage resources to Azure using Azure CLI. Azure CLI (Azure Cloud Shell) is used to create the required resources. Azure CLI is the free Bash shell which can be run directly within the Azure portal. That is preinstalled and configured within the account. Perform the below steps one by one to achieve this.
Use Azure CLI (Azure Cloud Shell)
It is a free Bash shell which can be run directly within the Azure portal. It is preinstalled and configured within an account.
Click Cloud Shell button on the menu bar at the top right side of the Azure portal.
It will launch an interactive shell which can be used to run the steps outlined in this article. Select a Bash environment. To use code editor in cloud shell toolbar go to Settings menu and then select Go to Classic version.
Create resource group - Resource group is a logical container used to hold related resources. In it include the resources which you want to manage as a group. App configuration resource belongs to this resource group. Create it using az group create command.
az group create \
--name myResourceGroup1 \
--location eastus2 eastus2 is location code instead of it one can use their nearest region location.
Create variables - Here, several commands require unique names/values with same parameters. Hence, for easy to manage let's create some variables, assign values and reuse them in the command parameters. let command is used as below to create required variables.
let resourceGroup=myResourceGroup1 \
location=eastus2 \
storageAccountName=myStorageAccountName$RANDOMCreate storage account - Storage account contains all azure storage data objects, such as blobs, files, queues, and tables. It is an Azure Resource Manager resource. It provides unique namespace for Azure Storage data. Create it using az storage account create command. Using this storage account all the activities will be performed in below console application section. It will take few minutes to complete the operation process.
az storage account create \
--resource-group $resourceGroup \
--name $storageAccountName
--location $location
--sku Standard_LRSAssign role to Microsoft Entra (Azure AD) user
Let’s assign Microsoft Entra user to “Storage Queue Data Contributor” role which gives user account permission to create queues, send and receive messages using Azure RBAC. It is important to assign roles to Microsoft Entra (Azure AD) user name. Roles will define what access and permissions a user has within an organization’s Microsoft cloud environment. Roles determine what resources and actions a user can perform. “Storage Queue Data Contributor” role is to be assigned to Microsoft Entra user which allows application to send and receive messages. One by one perform below steps in Azure CLI.
Retrieve userPrincipalName
First, retrieve userPrincipalName from account as mentioned in following command. It retrieves the information about currently authenticated user including their UPN and represents that who the role will be assigned to.
userPrincipal=$(az rest
--method GET
--url https://graph.microsoft.com/v1.0/me \
--headers 'Content-Type=application/json' \
--query userPrincipalName
--output tsv)Retrieve resource ID
Second, get the resource ID of storage account using below command. This will be used in next step to set scope for role assignment. Resource ID sets scope for role assignment to specific namespace.
resourceID=$(az storage account show \
--resource-group $resourceGroup \
--name $storageAccountName\
--query id \
--output tsv)Create and assign “Storage Queue Data Contributor” role
Finally, create and assign “Storage Queue Data Contributor” role using az role assignment create command. Command execution will create and assign “Storage Queue Data Contributor” role which gives permission to create queues, send and receive messages using Azure RBAC. This will give permissions to manage next routines. userPrincipal and resourceID variables are created in first and second steps which are used in this command to receive actual value at command execution time.
az role assignment create \
--assignee $userPrincipal\
--role "Storage Queue Data Contributor" \
--scope $resourceIDNow, required basic resources are deployed and ready to use in console application.
Send and Receive Messages using .NET Client Library
Let’s set-up the .NET console application to send and receive the messages using .NET Client Library. Perform the following steps one by one in to Cloud Shell to achieve this.
.NET Project Setup
Set up one console application to send and receive message using .NET Client Library.
Create project directory and change to that directory. First create a directory for the project using below command and move to that directory.
mkdir queuestorage1
cd queuestorage1Create .NET console application to add and fetch secrets through it. dotnet new console command is used to create a .NET console app.
dotnet new consoleInstall Packages - run dotnet add package command to add Azure.Identity and Azure.Storage.Queues packages in project to use the SDK.
dotnet add package Azure.Identity
dotnet add package Azure.Storage.QueuesStarter Code
Add the below starter code into the project and replace template code in Program.cs file by using editor in Cloud Shell.
Application editing - open Program.cs file to edit it in the Azure CLI using code command.
code Program.csCode replacing - replace existing code of Program.cs with the below code. This is the initial starter code and will replace last four commented lines with actual code in next “Complete code” section. Replace MY_STORAGE_ACCOUNT_NAME with actual storage account name.

Join the conversation! Your thoughts help the community grow.