Skip to content

Commit c022094

Browse files
authored
Merge pull request #227494 from pauljewellmsft/pauljewell-update-package-info
Add project setup info to dev article
2 parents d706fc0 + 4ed499b commit c022094

File tree

1 file changed

+77
-14
lines changed

1 file changed

+77
-14
lines changed

articles/storage/blobs/storage-blob-user-delegation-sas-create-dotnet.md

Lines changed: 77 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,77 @@ ms.devlang: csharp
1818

1919
[!INCLUDE [storage-auth-sas-intro-include](../../../includes/storage-auth-sas-intro-include.md)]
2020

21-
This article shows how to use Azure Active Directory (Azure AD) credentials to create a user delegation SAS for a container, directory, or blob with the Blob Storage client library for .NET version 12.
21+
This article shows how to use Azure Active Directory (Azure AD) credentials to create a user delegation SAS for a container, directory, or blob with the Blob Storage client library for .NET.
2222

2323
[!INCLUDE [storage-auth-user-delegation-include](../../../includes/storage-auth-user-delegation-include.md)]
2424

2525
## Assign Azure roles for access to data
2626

2727
When an Azure AD security principal attempts to access blob data, that security principal must have permissions to the resource. Whether the security principal is a managed identity in Azure or an Azure AD user account running code in the development environment, the security principal must be assigned an Azure role that grants access to blob data. For information about assigning permissions via Azure RBAC, see [Assign an Azure role for access to blob data](assign-azure-role-data-access.md).
2828

29+
## Set up your project
30+
31+
To work with the code examples in this article, follow these steps to set up your project.
32+
33+
### Install packages
34+
35+
For the [blob](#get-a-user-delegation-sas-for-a-blob) and [container](#get-a-user-delegation-sas-for-a-container) code examples, add the following packages:
36+
37+
### [.NET CLI](#tab/packages-dotnetcli)
38+
39+
```dotnetcli
40+
dotnet add package Azure.Identity
41+
dotnet add package Azure.Storage.Blobs
42+
```
43+
44+
### [PowerShell](#tab/packages-powershell)
45+
46+
```powershell
47+
Install-Package Azure.Identity
48+
Install-Package Azure.Storage.Blobs
49+
```
50+
---
51+
52+
For the [directory](#get-a-user-delegation-sas-for-a-directory) code examples, add the following packages:
53+
54+
### [.NET CLI](#tab/packages-dotnetcli)
55+
56+
```dotnetcli
57+
dotnet add package Azure.Identity
58+
dotnet add package Azure.Storage.Files.DataLake
59+
```
60+
61+
### [PowerShell](#tab/packages-powershell)
62+
63+
```powershell
64+
Install-Package Azure.Identity
65+
Install-Package Azure.Storage.Files.DataLake
66+
```
67+
---
68+
69+
### Set up the app code
70+
71+
For the [blob](#get-a-user-delegation-sas-for-a-blob) and [container](#get-a-user-delegation-sas-for-a-container) code examples, add the following `using` directives:
72+
73+
```csharp
74+
using Azure;
75+
using Azure.Identity;
76+
using Azure.Storage.Blobs;
77+
using Azure.Storage.Blobs.Models;
78+
using Azure.Storage.Blobs.Specialized;
79+
using Azure.Storage.Sas;
80+
```
81+
82+
For the [directory](#get-a-user-delegation-sas-for-a-directory) code example, add the following `using` directives:
83+
84+
```csharp
85+
using Azure;
86+
using Azure.Identity;
87+
using Azure.Storage.Files.DataLake;
88+
using Azure.Storage.Files.DataLake.Models;
89+
using Azure.Storage.Sas;
90+
```
91+
2992
## Get an authenticated token credential
3093

3194
To get a token credential that your code can use to authorize requests to Blob Storage, create an instance of the [DefaultAzureCredential](/dotnet/api/azure.identity.defaultazurecredential) class. For more information about using the DefaultAzureCredential class to authorize a managed identity to access Blob Storage, see [Azure Identity client library for .NET](/dotnet/api/overview/azure/identity-readme).
@@ -34,11 +97,11 @@ The following code snippet shows how to get the authenticated token credential a
3497

3598
```csharp
3699
// Construct the blob endpoint from the account name.
37-
string blobEndpoint = string.Format("https://{0}.blob.core.windows.net", accountName);
100+
string blobEndpoint = $"https://{accountName}.blob.core.windows.net";
38101

39-
// Create a new Blob service client with Azure AD credentials.
40-
BlobServiceClient blobClient = new BlobServiceClient(new Uri(blobEndpoint),
41-
new DefaultAzureCredential());
102+
// Create a blob service client object using DefaultAzureCredential
103+
BlobServiceClient blobClient = new(new Uri(blobEndpoint),
104+
new DefaultAzureCredential());
42105
```
43106

44107
To learn more about authorizing access to Blob Storage from your applications with the .NET SDK, see [How to authenticate .NET applications with Azure services](/dotnet/azure/sdk/authentication).
@@ -57,19 +120,19 @@ Use one of the following methods to request the user delegation key:
57120
The following code snippet gets the user delegation key and writes out its properties:
58121

59122
```csharp
60-
// Get a user delegation key for the Blob service that's valid for seven days.
61-
// You can use the key to generate any number of shared access signatures over the lifetime of the key.
123+
// Get a user delegation key for the Blob service that's valid for seven days
124+
// You can use the key to generate any number of shared access signatures over the lifetime of the key
62125
UserDelegationKey key = await blobClient.GetUserDelegationKeyAsync(DateTimeOffset.UtcNow,
63126
DateTimeOffset.UtcNow.AddDays(7));
64127

65-
// Read the key's properties.
128+
// Read the key's properties
66129
Console.WriteLine("User delegation key properties:");
67-
Console.WriteLine("Key signed start: {0}", key.SignedStartsOn);
68-
Console.WriteLine("Key signed expiry: {0}", key.SignedExpiresOn);
69-
Console.WriteLine("Key signed object ID: {0}", key.SignedObjectId);
70-
Console.WriteLine("Key signed tenant ID: {0}", key.SignedTenantId);
71-
Console.WriteLine("Key signed service: {0}", key.SignedService);
72-
Console.WriteLine("Key signed version: {0}", key.SignedVersion);
130+
Console.WriteLine($"Key signed start: {key.SignedStartsOn}");
131+
Console.WriteLine($"Key signed expiry: {key.SignedExpiresOn}");
132+
Console.WriteLine($"Key signed object ID: {key.SignedObjectId}");
133+
Console.WriteLine($"Key signed tenant ID: {key.SignedTenantId}");
134+
Console.WriteLine($"Key signed service: {key.SignedService}");
135+
Console.WriteLine($"Key signed version: {key.SignedVersion}");
73136
```
74137

75138
## Get a user delegation SAS for a blob

0 commit comments

Comments
 (0)