Skip to content

Commit c64ea52

Browse files
authored
Merge pull request #97689 from tamram/tamram-1203
update user delegation SAS code for .NET release
2 parents 43f028a + 466aa28 commit c64ea52

File tree

1 file changed

+48
-34
lines changed

1 file changed

+48
-34
lines changed

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

Lines changed: 48 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
---
22
title: Create a user delegation SAS for a container or blob with .NET (preview) - Azure Storage
3-
description: Learn how to create a user delegation SAS using Azure Active Directory credentials in Azure Storage using the .NET client library.
3+
description: Learn how to create a user delegation SAS (preview) using Azure Active Directory credentials in Azure Storage using the .NET client library.
44
services: storage
55
author: tamram
66

77
ms.service: storage
88
ms.topic: conceptual
9-
ms.date: 10/17/2019
9+
ms.date: 12/03/2019
1010
ms.author: tamram
1111
ms.reviewer: cbrooks
1212
ms.subservice: blobs
@@ -16,13 +16,13 @@ ms.subservice: blobs
1616

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

19-
This article shows how to use Azure Active Directory (Azure AD) credentials to create a user delegation SAS for a container or blob with the Azure Storage client library for .NET.
19+
This article shows how to use Azure Active Directory (Azure AD) credentials to create a user delegation SAS (preview) for a container or blob with the Azure Storage client library for .NET.
2020

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

23-
## Authenticate with the Azure Identity library (preview)
23+
## Authenticate with the Azure Identity library
2424

25-
The Azure Identity client library for .NET (preview) authenticates a security principal. When your code is running in Azure, the security principal is a managed identity for Azure resources.
25+
The Azure Identity client library for .NET authenticates a security principal. When your code is running in Azure, the security principal is a managed identity for Azure resources.
2626

2727
When your code is running in the development environment, authentication may be handled automatically, or it may require a browser login, depending on which tools you're using. Microsoft Visual Studio supports single sign-on (SSO), so that the active Azure AD user account is automatically used for authentication. For more information about SSO, see [Single sign-on to applications](../../active-directory/manage-apps/what-is-single-sign-on.md).
2828

@@ -36,30 +36,30 @@ For more information about the Azure Identity client library, see [Azure Identit
3636

3737
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 RBAC role that grants access to blob data in Azure Storage. For information about assigning permissions via RBAC, see the section titled **Assign RBAC roles for access rights** in [Authorize access to Azure blobs and queues using Azure Active Directory](../common/storage-auth-aad.md#assign-rbac-roles-for-access-rights).
3838

39-
## Install the preview packages
39+
## Install the packages
4040

41-
The examples in this article use the latest preview version of the [Azure Storage client library for Blob storage](https://www.nuget.org/packages/Azure.Storage.Blobs). To install the preview package, run the following command from the NuGet package manager console:
41+
The examples in this article use the latest version of the [Azure Storage client library for Blob storage](https://www.nuget.org/packages/Azure.Storage.Blobs). To install the package, run the following command from the NuGet package manager console:
4242

4343
```powershell
44-
Install-Package Azure.Storage.Blobs -IncludePrerelease
44+
Install-Package Azure.Storage.Blobs
4545
```
4646

47-
The examples in this article also use the latest preview version of the [Azure Identity client library for .NET](https://www.nuget.org/packages/Azure.Identity/) to authenticate with Azure AD credentials. To install the preview package, run the following command from the NuGet package manager console:
47+
The examples in this article also use the latest version of the [Azure Identity client library for .NET](https://www.nuget.org/packages/Azure.Identity/) to authenticate with Azure AD credentials. To install the package, run the following command from the NuGet package manager console:
4848

4949
```powershell
50-
Install-Package Azure.Identity -IncludePrerelease
50+
Install-Package Azure.Identity
5151
```
5252

5353
## Add using directives
5454

55-
Add the following `using` directives to your code to use the preview versions of the Azure Identity and Azure Storage client libraries.
55+
Add the following `using` directives to your code to use the Azure Identity and Azure Storage client libraries.
5656

5757
```csharp
5858
using System;
5959
using System.IO;
6060
using System.Threading.Tasks;
61+
using Azure;
6162
using Azure.Identity;
62-
using Azure.Storage;
6363
using Azure.Storage.Sas;
6464
using Azure.Storage.Blobs;
6565
using Azure.Storage.Blobs.Models;
@@ -72,8 +72,10 @@ To get a token credential that your code can use to authorize requests to Azure
7272
The following code snippet shows how to get the authenticated token credential and use it to create a service client for Blob storage:
7373

7474
```csharp
75+
// Construct the blob endpoint from the account name.
7576
string blobEndpoint = string.Format("https://{0}.blob.core.windows.net", accountName);
7677

78+
// Create a new Blob service client with Azure AD credentials.
7779
BlobServiceClient blobClient = new BlobServiceClient(new Uri(blobEndpoint),
7880
new DefaultAzureCredential());
7981
```
@@ -92,14 +94,17 @@ Use one of the following methods to request the user delegation key:
9294
The following code snippet gets the user delegation key and writes out its properties:
9395

9496
```csharp
97+
// Get a user delegation key for the Blob service that's valid for seven days.
98+
// You can use the key to generate any number of shared access signatures over the lifetime of the key.
9599
UserDelegationKey key = await blobClient.GetUserDelegationKeyAsync(DateTimeOffset.UtcNow,
96100
DateTimeOffset.UtcNow.AddDays(7));
97101

102+
// Read the key's properties.
98103
Console.WriteLine("User delegation key properties:");
99-
Console.WriteLine("Key signed start: {0}", key.SignedStart);
100-
Console.WriteLine("Key signed expiry: {0}", key.SignedExpiry);
101-
Console.WriteLine("Key signed object ID: {0}", key.SignedOid);
102-
Console.WriteLine("Key signed tenant ID: {0}", key.SignedTid);
104+
Console.WriteLine("Key signed start: {0}", key.SignedStartsOn);
105+
Console.WriteLine("Key signed expiry: {0}", key.SignedExpiresOn);
106+
Console.WriteLine("Key signed object ID: {0}", key.SignedObjectId);
107+
Console.WriteLine("Key signed tenant ID: {0}", key.SignedTenantId);
103108
Console.WriteLine("Key signed service: {0}", key.SignedService);
104109
Console.WriteLine("Key signed version: {0}", key.SignedVersion);
105110
```
@@ -109,18 +114,23 @@ Console.WriteLine("Key signed version: {0}", key.SignedVersion);
109114
The following code snippet shows create a new [BlobSasBuilder](/dotnet/api/azure.storage.sas.blobsasbuilder) and provide the parameters for the user delegation SAS. The snippet then calls the [ToSasQueryParameters](/dotnet/api/azure.storage.sas.blobsasbuilder.tosasqueryparameters) to get the SAS token string. Finally, the code builds the complete URI, including the resource address and SAS token.
110115

111116
```csharp
112-
BlobSasBuilder builder = new BlobSasBuilder()
117+
// Create a SAS token that's valid for one hour.
118+
BlobSasBuilder sasBuilder = new BlobSasBuilder()
113119
{
114-
ContainerName = containerName,
120+
BlobContainerName = containerName,
115121
BlobName = blobName,
116-
Permissions = "r",
117122
Resource = "b",
118-
StartTime = DateTimeOffset.UtcNow,
119-
ExpiryTime = DateTimeOffset.UtcNow.AddMinutes(5)
123+
StartsOn = DateTimeOffset.UtcNow,
124+
ExpiresOn = DateTimeOffset.UtcNow.AddHours(1)
120125
};
121126

127+
// Specify read permissions for the SAS.
128+
sasBuilder.SetPermissions(BlobSasPermissions.Read);
129+
130+
// Use the key to get the SAS token.
122131
string sasToken = sasBuilder.ToSasQueryParameters(key, accountName).ToString();
123132

133+
// Construct the full URI, including the SAS token.
124134
UriBuilder fullUri = new UriBuilder()
125135
{
126136
Scheme = "https",
@@ -145,30 +155,33 @@ async static Task<Uri> GetUserDelegationSasBlob(string accountName, string conta
145155
new DefaultAzureCredential());
146156

147157
// Get a user delegation key for the Blob service that's valid for seven days.
148-
// Use the key to generate any number of shared access signatures over the lifetime of the key.
149-
UserDelegationKey key = await blobClient.GetUserDelegationKeyAsync(DateTimeOffset.UtcNow,
150-
DateTimeOffset.UtcNow.AddDays(7));
158+
// You can use the key to generate any number of shared access signatures over the lifetime of the key.
159+
UserDelegationKey key = await blobClient.GetUserDelegationKeyAsync(DateTimeOffset.UtcNow,
160+
DateTimeOffset.UtcNow.AddDays(7));
151161

152162
// Read the key's properties.
153163
Console.WriteLine("User delegation key properties:");
154-
Console.WriteLine("Key signed start: {0}", key.SignedStart);
155-
Console.WriteLine("Key signed expiry: {0}", key.SignedExpiry);
156-
Console.WriteLine("Key signed object ID: {0}", key.SignedOid);
157-
Console.WriteLine("Key signed tenant ID: {0}", key.SignedTid);
164+
Console.WriteLine("Key signed start: {0}", key.SignedStartsOn);
165+
Console.WriteLine("Key signed expiry: {0}", key.SignedExpiresOn);
166+
Console.WriteLine("Key signed object ID: {0}", key.SignedObjectId);
167+
Console.WriteLine("Key signed tenant ID: {0}", key.SignedTenantId);
158168
Console.WriteLine("Key signed service: {0}", key.SignedService);
159169
Console.WriteLine("Key signed version: {0}", key.SignedVersion);
170+
Console.WriteLine();
160171

161-
// Create a SAS token that's valid a short interval.
172+
// Create a SAS token that's valid for one hour.
162173
BlobSasBuilder sasBuilder = new BlobSasBuilder()
163174
{
164-
ContainerName = containerName,
175+
BlobContainerName = containerName,
165176
BlobName = blobName,
166-
Permissions = "r",
167177
Resource = "b",
168-
StartTime = DateTimeOffset.UtcNow,
169-
ExpiryTime = DateTimeOffset.UtcNow.AddMinutes(5)
178+
StartsOn = DateTimeOffset.UtcNow,
179+
ExpiresOn = DateTimeOffset.UtcNow.AddHours(1)
170180
};
171181

182+
// Specify read permissions for the SAS.
183+
sasBuilder.SetPermissions(BlobSasPermissions.Read);
184+
172185
// Use the key to get the SAS token.
173186
string sasToken = sasBuilder.ToSasQueryParameters(key, accountName).ToString();
174187

@@ -182,6 +195,7 @@ async static Task<Uri> GetUserDelegationSasBlob(string accountName, string conta
182195
};
183196

184197
Console.WriteLine("User delegation SAS URI: {0}", fullUri);
198+
Console.WriteLine();
185199
return fullUri.Uri;
186200
}
187201
```
@@ -216,7 +230,7 @@ private static async Task ReadBlobWithSasAsync(Uri sasUri)
216230
Console.WriteLine("Read operation succeeded for SAS {0}", sasUri);
217231
Console.WriteLine();
218232
}
219-
catch (StorageRequestFailedException e)
233+
catch (RequestFailedException e)
220234
{
221235
// Check for a 403 (Forbidden) error. If the SAS is invalid,
222236
// Azure Storage returns this error.

0 commit comments

Comments
 (0)