Skip to content

Commit 27ff78c

Browse files
authored
Merge pull request #221049 from alexwolfmsft/storage-queues-passwordless
first draft
2 parents 41e8aad + c6a4acf commit 27ff78c

File tree

2 files changed

+170
-41
lines changed

2 files changed

+170
-41
lines changed

articles/storage/queues/storage-quickstart-queues-dotnet.md

Lines changed: 96 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
---
2-
title: "Quickstart: Azure Queue Storage client library v12 - .NET"
3-
description: Learn how to use the Azure Queue Storage client library v12 for .NET to create a queue and add messages to the queue. Next, you learn how to read and delete messages from the queue. You'll also learn how to delete a queue.
2+
title: "Quickstart: Azure Queue Storage client library - .NET"
3+
description: Learn how to use the Azure Queue Storage client library for .NET to create a queue and add messages to the queue. Next, you learn how to read and delete messages from the queue. You'll also learn how to delete a queue.
44
author: normesta
55
ms.author: normesta
66
ms.date: 07/24/2020
77
ms.topic: quickstart
88
ms.service: storage
99
ms.subservice: queues
1010
ms.devlang: csharp
11-
ms.custom: devx-track-csharp, mode-api
11+
ms.custom: devx-track-csharp, mode-api, passwordless-dotnet
1212
---
1313

14-
# Quickstart: Azure Queue Storage client library v12 for .NET
14+
# Quickstart: Azure Queue Storage client library for .NET
1515

1616
Get started with the Azure Queue Storage client library version 12 for .NET. Azure Queue Storage is a service for storing large numbers of messages for later retrieval and processing. Follow these steps to install the package and try out example code for basic tasks.
1717

18-
Use the Azure Queue Storage client library v12 for .NET to:
18+
Use the Azure Queue Storage client library for .NET to:
1919

2020
- Create a queue
2121
- Add messages to a queue
@@ -40,22 +40,22 @@ Additional resources:
4040

4141
## Setting up
4242

43-
This section walks you through preparing a project to work with the Azure Queue Storage client library v12 for .NET.
43+
This section walks you through preparing a project to work with the Azure Queue Storage client library for .NET.
4444

4545
### Create the project
4646

47-
Create a .NET Core application named `QueuesQuickstartV12`.
47+
Create a .NET Core application named `QueuesQuickstart`.
4848

49-
1. In a console window (such as cmd, PowerShell, or Bash), use the `dotnet new` command to create a new console app with the name `QueuesQuickstartV12`. This command creates a simple "hello world" C# project with a single source file named `Program.cs`.
49+
1. In a console window (such as cmd, PowerShell, or Bash), use the `dotnet new` command to create a new console app with the name `QueuesQuickstart`. This command creates a simple "hello world" C# project with a single source file named `Program.cs`.
5050

5151
```console
52-
dotnet new console -n QueuesQuickstartV12
52+
dotnet new console -n QueuesQuickstart
5353
```
5454

55-
1. Switch to the newly created `QueuesQuickstartV12` directory.
55+
1. Switch to the newly created `QueuesQuickstart` directory.
5656

5757
```console
58-
cd QueuesQuickstartV12
58+
cd QueuesQuickstart
5959
```
6060

6161
### Install the package
@@ -68,14 +68,9 @@ dotnet add package Azure.Storage.Queues
6868

6969
### Set up the app framework
7070

71-
From the project directory:
72-
73-
1. Open the `Program.cs` file in your editor
74-
1. Remove the `Console.WriteLine("Hello, World");` statement
75-
1. Add `using` directives
76-
1. Update the `Main` method declaration to [support async code](/dotnet/csharp/whats-new/csharp-7#async-main)
77-
78-
Here's the code:
71+
1. Open the project in your editor of choice
72+
1. Open the `program.cs` file
73+
1. Update the existing code to match the following:
7974

8075
```csharp
8176
using Azure;
@@ -84,19 +79,26 @@ using Azure.Storage.Queues.Models;
8479
using System;
8580
using System.Threading.Tasks;
8681

87-
namespace QueuesQuickstartV12
88-
{
89-
class Program
90-
{
91-
static async Task Main(string[] args)
92-
{
93-
}
94-
}
95-
}
82+
Console.WriteLine("Hello, World!");
83+
9684
```
9785

86+
## Authenticate to Azure
87+
88+
[!INCLUDE [passwordless-overview](../../../includes/passwordless/passwordless-overview.md)]
89+
90+
## [Passwordless (Recommended)](#tab/passwordless)
91+
92+
[!INCLUDE [dotnet-default-azure-credential-overview](../../../includes/passwordless/dotnet-default-azure-credential-overview.md)]
93+
94+
[!INCLUDE [storage-queues-create-assign-roles](../../../includes/passwordless/storage-queues/storage-queues-assign-roles.md)]
95+
96+
## [Connection String](#tab/connection-string)
97+
9898
[!INCLUDE [storage-quickstart-credentials-include](../../../includes/storage-quickstart-credentials-include.md)]
9999

100+
---
101+
100102
## Object model
101103

102104
Azure Queue Storage is a service for storing large numbers of messages. A queue message can be up to 64 KB in size. A queue may contain millions of messages, up to the total capacity limit of a storage account. Queues are commonly used to create a backlog of work to process asynchronously. Queue Storage offers three types of resources:
@@ -119,23 +121,71 @@ Use the following .NET classes to interact with these resources:
119121

120122
These example code snippets show you how to perform the following actions with the Azure Queue Storage client library for .NET:
121123

122-
- [Get the connection string](#get-the-connection-string)
123-
- [Create a queue](#create-a-queue)
124+
- [Authenticate and create the client](#add-the-azure-identity-client-library)
125+
- [Create a queue](#create-a-queue-using-the-queueclient)
124126
- [Add messages to a queue](#add-messages-to-a-queue)
125127
- [Peek at messages in a queue](#peek-at-messages-in-a-queue)
126128
- [Update a message in a queue](#update-a-message-in-a-queue)
127129
- [Receive messages from a queue](#receive-messages-from-a-queue)
128130
- [Delete messages from a queue](#delete-messages-from-a-queue)
129131
- [Delete a queue](#delete-a-queue)
130132

133+
## [Passwordless (Recommended)](#tab/passwordless)
134+
135+
### Add the Azure Identity client library
136+
137+
[!INCLUDE [default-azure-credential-sign-in](../../../includes/passwordless/default-azure-credential-sign-in.md)]
138+
139+
You can authenticate a `QueueClient` to Storage Queue using `DefaultAzureCredential` by adding the `Azure.Identity` NuGet package to your application. `DefaultAzureCredential` will automatically discover and use the account you signed-in with in the previous step.
140+
141+
```dotnetcli
142+
dotnet add package Azure.Identity
143+
```
144+
145+
At the top of the `Program.cs` file, add a using directive for the `Azure.Identity` namespace.
146+
147+
```csharp
148+
using Azure.Identity
149+
```
150+
151+
### Create a queue using the QueueClient
152+
153+
Decide on a name for the new queue. The following code appends a GUID value to the queue name to ensure that it's unique.
154+
155+
> [!IMPORTANT]
156+
> Queue names may only contain lowercase letters, numbers, and hyphens, and must begin with a letter or a number. Each hyphen must be preceded and followed by a non-hyphen character. The name must also be between 3 and 63 characters long. For more information, see [Naming queues and metadata](/rest/api/storageservices/naming-queues-and-metadata).
157+
158+
Create an instance of the [`QueueClient`](/dotnet/api/azure.storage.queues.queueclient) class. Then, call the [`CreateAsync`](/dotnet/api/azure.storage.queues.queueclient.createasync) method to create the queue in your storage account.
159+
160+
Add the code below to the end of the `Program.cs` file. Make sure to replace the `"<your-storage-account-name>` placeholder value.
161+
162+
```csharp
163+
// Create a unique name for the queue
164+
// TODO: Replace the <your-storage-account-name> placeholder
165+
string queueName = "quickstartqueues-" + Guid.NewGuid().ToString();
166+
string storageAccountName = "<your-storage-account-name>";
167+
168+
Console.WriteLine($"Creating queue: {queueName}");
169+
170+
// Instantiate a QueueClient to create and manipulate the queue
171+
QueueClient queueClient = new QueueClient(
172+
new Uri($"https://{storageAccountName}.queue.core.windows.net/{queueName}"),
173+
new DefaultAzureCredential());
174+
175+
// Create the queue
176+
await queueClient.CreateAsync();
177+
```
178+
179+
## [Connection String](#tab/connection-string)
180+
131181
### Get the connection string
132182

133183
The following code retrieves the connection string for the storage account. The connection string is stored in the environment variable created in the [Configure your storage connection string](#configure-your-storage-connection-string) section.
134184

135-
Add this code inside the `Main` method:
185+
Add this code to the end of the `Program.cs` file:
136186

137187
```csharp
138-
Console.WriteLine("Azure Queue Storage client library v12 - .NET quickstart sample\n");
188+
Console.WriteLine("Azure Queue Storage client library - .NET quickstart sample\n");
139189

140190
// Retrieve the connection string for use with the application. The storage
141191
// connection string is stored in an environment variable called
@@ -146,7 +196,7 @@ Console.WriteLine("Azure Queue Storage client library v12 - .NET quickstart samp
146196
string connectionString = Environment.GetEnvironmentVariable("AZURE_STORAGE_CONNECTION_STRING");
147197
```
148198

149-
### Create a queue
199+
### Create a queue using the QueueClient
150200

151201
Decide on a name for the new queue. The following code appends a GUID value to the queue name to ensure that it's unique.
152202

@@ -155,7 +205,7 @@ Decide on a name for the new queue. The following code appends a GUID value to t
155205
156206
Create an instance of the [`QueueClient`](/dotnet/api/azure.storage.queues.queueclient) class. Then, call the [`CreateAsync`](/dotnet/api/azure.storage.queues.queueclient.createasync) method to create the queue in your storage account.
157207

158-
Add this code to the end of the `Main` method:
208+
Add this code to the end of the `Program.cs` file:
159209

160210
```csharp
161211
// Create a unique name for the queue
@@ -171,11 +221,16 @@ QueueClient queueClient = new QueueClient(connectionString, queueName);
171221
await queueClient.CreateAsync();
172222
```
173223

224+
> [!IMPORTANT]
225+
> The account access key should be used with caution. If your account access key is lost or accidentally placed in an insecure location, your service may become vulnerable. Anyone who has the access key is able to authorize requests against the storage account, and effectively has access to all the data. `DefaultAzureCredential` provides enhanced security features and benefits and is the recommended approach for managing authorization to Azure services
226+
227+
---
228+
174229
### Add messages to a queue
175230

176231
The following code snippet asynchronously adds messages to queue by calling the [`SendMessageAsync`](/dotnet/api/azure.storage.queues.queueclient.sendmessageasync) method. It also saves a [`SendReceipt`](/dotnet/api/azure.storage.queues.models.sendreceipt) returned from a `SendMessageAsync` call. The receipt is used to update the message later in the program.
177232

178-
Add this code to the end of the `Main` method:
233+
Add this code to the end of the `Program.cs` file:
179234

180235
```csharp
181236
Console.WriteLine("\nAdding messages to the queue...");
@@ -192,7 +247,7 @@ SendReceipt receipt = await queueClient.SendMessageAsync("Third message");
192247

193248
Peek at the messages in the queue by calling the [`PeekMessagesAsync`](/dotnet/api/azure.storage.queues.queueclient.peekmessagesasync) method. This method retrieves one or more messages from the front of the queue but doesn't alter the visibility of the message.
194249

195-
Add this code to the end of the `Main` method:
250+
Add this code to the end of the `Program.cs` file:
196251

197252
```csharp
198253
Console.WriteLine("\nPeek at the messages in the queue...");
@@ -222,7 +277,7 @@ await queueClient.UpdateMessageAsync(receipt.MessageId, receipt.PopReceipt, "Thi
222277

223278
Download previously added messages by calling the [`ReceiveMessagesAsync`](/dotnet/api/azure.storage.queues.queueclient.receivemessagesasync) method.
224279

225-
Add this code to the end of the `Main` method:
280+
Add this code to the end of the `Program.cs` file:
226281

227282
```csharp
228283
Console.WriteLine("\nReceiving messages from the queue...");
@@ -233,11 +288,11 @@ QueueMessage[] messages = await queueClient.ReceiveMessagesAsync(maxMessages: 10
233288

234289
### Delete messages from a queue
235290

236-
Delete messages from the queue after they're been processed. In this case, processing is just displaying the message on the console.
291+
Delete messages from the queue after they've been processed. In this case, processing is just displaying the message on the console.
237292

238293
The app pauses for user input by calling `Console.ReadLine` before it processes and deletes the messages. Verify in your [Azure portal](https://portal.azure.com) that the resources were created correctly, before they're deleted. Any messages not explicitly deleted will eventually become visible in the queue again for another chance to process them.
239294

240-
Add this code to the end of the `Main` method:
295+
Add this code to the end of the `Program.cs` file:
241296

242297
```csharp
243298
Console.WriteLine("\nPress Enter key to 'process' messages and delete them from the queue...");
@@ -259,7 +314,7 @@ foreach (QueueMessage message in messages)
259314

260315
The following code cleans up the resources the app created by deleting the queue using the [`DeleteAsync`](/dotnet/api/azure.storage.queues.queueclient.deleteasync) method.
261316

262-
Add this code to the end of the `Main` method:
317+
Add this code to the end of the `Program.cs` file:
263318

264319
```csharp
265320
Console.WriteLine("\nPress Enter key to delete the queue...");
@@ -289,7 +344,7 @@ dotnet run
289344
The output of the app is similar to the following example:
290345

291346
```output
292-
Azure Queue Storage client library v12 - .NET quickstart sample
347+
Azure Queue Storage client library - .NET quickstart sample
293348
294349
Creating queue: quickstartqueues-5c72da2c-30cc-4f09-b05c-a95d9da52af2
295350
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
---
2+
title: "include file"
3+
description: "include file"
4+
services: storage
5+
author: alexwolfmsft
6+
ms.service: storage
7+
ms.topic: include
8+
ms.date: 10/11/2022
9+
ms.author: alexwolf
10+
ms.custom: include file
11+
---
12+
13+
When developing locally, make sure that the user account that is accessing the queue data has the correct permissions. You'll need **Storage Queue Data Contributor** to read and write queue data. To assign yourself this role, you'll need to be assigned the **User Access Administrator** role, or another role that includes the **Microsoft.Authorization/roleAssignments/write** action. You can assign Azure RBAC roles to a user using the Azure portal, Azure CLI, or Azure PowerShell. You can learn more about the available scopes for role assignments on the [scope overview](../../../articles/role-based-access-control/scope-overview.md) page.
14+
15+
In this scenario, you'll assign permissions to your user account, scoped to the storage account, to follow the [Principle of Least Privilege](../../../articles/active-directory/develop/secure-least-privileged-access.md). This practice gives users only the minimum permissions needed and creates more secure production environments.
16+
17+
The following example will assign the **Storage Queue Data Contributor** role to your user account, which provides both read and write access to queue data in your storage account.
18+
19+
> [!IMPORTANT]
20+
> In most cases it will take a minute or two for the role assignment to propagate in Azure, but in rare cases it may take up to eight minutes. If you receive authentication errors when you first run your code, wait a few moments and try again.
21+
22+
### [Azure portal](#tab/roles-azure-portal)
23+
24+
1. In the Azure portal, locate your storage account using the main search bar or left navigation.
25+
26+
2. On the storage account overview page, select **Access control (IAM)** from the left-hand menu.
27+
28+
3. On the **Access control (IAM)** page, select the **Role assignments** tab.
29+
30+
4. Select **+ Add** from the top menu and then **Add role assignment** from the resulting drop-down menu.
31+
32+
:::image type="content" source="../../../articles/storage/common/media/assign-role-system-identity.png" alt-text="A screenshot showing how to assign a role.":::
33+
34+
5. Use the search box to filter the results to the desired role. For this example, search for *Storage Queue Data Contributor* and select the matching result and then choose **Next**.
35+
36+
6. Under **Assign access to**, select **User, group, or service principal**, and then choose **+ Select members**.
37+
38+
7. In the dialog, search for your Azure AD username (usually your *user@domain* email address) and then choose **Select** at the bottom of the dialog.
39+
40+
8. Select **Review + assign** to go to the final page, and then **Review + assign** again to complete the process.
41+
42+
### [Azure CLI](#tab/roles-azure-cli)
43+
44+
To assign a role at the resource level using the Azure CLI, you first must retrieve the resource id using the `az storage account show` command. You can filter the output properties using the `--query` parameter.
45+
46+
```azurecli
47+
az storage account show --resource-group '<your-resource-group-name>' --name '<your-storage-account-name>' --query id
48+
```
49+
50+
Copy the output `Id` from the preceding command. You can then assign roles using the [az role](/cli/azure/role) command of the Azure CLI.
51+
52+
```azurecli
53+
az role assignment create --assignee "<user@domain>" \
54+
--role "Storage Queue Data Contributor" \
55+
--scope "<your-resource-id>"
56+
```
57+
58+
### [PowerShell](#tab/roles-powershell)
59+
60+
To assign a role at the resource level using Azure PowerShell, you first must retrieve the resource ID using the `Get-AzResource` command.
61+
62+
```azurepowershell
63+
Get-AzResource -ResourceGroupName "<yourResourceGroupname>" -Name "<yourStorageAccountName>"
64+
```
65+
66+
Copy the `Id` value from the preceding command output. You can then assign roles using the [New-AzRoleAssignment](/powershell/module/az.resources/new-azroleassignment) command in PowerShell.
67+
68+
```azurepowershell
69+
New-AzRoleAssignment -SignInName <user@domain> `
70+
-RoleDefinitionName "Storage Queue Data Contributor" `
71+
-Scope <yourStorageAccountId>
72+
```
73+
74+
---

0 commit comments

Comments
 (0)