Skip to content

Commit 14ca90a

Browse files
authored
Merge pull request #219991 from pauljewellmsft/pauljewell-get-started-dotnet
Update auth section for .NET dev guide
2 parents ce21d36 + d78fe51 commit 14ca90a

File tree

1 file changed

+33
-46
lines changed

1 file changed

+33
-46
lines changed

articles/storage/blobs/storage-blob-dotnet-get-started.md

Lines changed: 33 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,37 @@ using Azure.Storage.Blobs.Specialized;
5353

5454
- [Azure.Storage.Blobs.Models](/dotnet/api/azure.storage.blobs.models): All other utility classes, structures, and enumeration types.
5555

56-
## Connect to Blob Storage
56+
## Authorize access and connect to Blob Storage
5757

58-
To connect to Blob Storage, create an instance of the [BlobServiceClient](/dotnet/api/azure.storage.blobs.blobserviceclient) class. This object is your starting point. You can use it to operate on the blob service instance and its containers. You can create a [BlobServiceClient](/dotnet/api/azure.storage.blobs.blobserviceclient) by using an account access key, a shared access signature (SAS), or by using an Azure Active Directory (Azure AD) authorization token.
58+
To connect to Blob Storage, create an instance of the [BlobServiceClient](/dotnet/api/azure.storage.blobs.blobserviceclient) class. This object is your starting point. You can use it to operate on the blob service instance and its containers. You can authorize access and create a [BlobServiceClient](/dotnet/api/azure.storage.blobs.blobserviceclient) object by using an Azure Active Directory (Azure AD) authorization token, an account access key, or a shared access signature (SAS).
5959

60-
To learn more about each of these authorization mechanisms, see [Authorize access to data in Azure Storage](../common/authorize-data-access.md).
60+
## [Azure AD](#tab/azure-ad)
61+
62+
To authorize with Azure AD, you'll need to use a security principal. The type of security principal you need depends on where your application runs. Use this table as a guide.
63+
64+
| Where the application runs | Security principal | Guidance |
65+
| --- | --- | --- |
66+
| Local machine (developing and testing) | Service principal | In this method, dedicated **application service principal** objects are set up using the App registration process for use during local development. The identity of the service principal is then stored as environment variables to be accessed by the app when it's run in local development.<br><br>This method allows you to assign the specific resource permissions needed by the app to the service principal objects used by developers during local development. This approach ensures the application only has access to the specific resources it needs and replicates the permissions the app will have in production.<br><br>The downside of this approach is the need to create separate service principal objects for each developer that works on an application.<br><br>[Authorize access using developer service principals](/dotnet/azure/sdk/authentication-local-development-service-principal?toc=/azure/storage/blobs/toc.json&bc=/azure/storage/blobs/breadcrumb/toc.json) |
67+
| Local machine (developing and testing) | User identity | In this method, a developer must be signed-in to Azure from either Visual Studio, the Azure Tools extension for VS Code, the Azure CLI, or Azure PowerShell on their local workstation. The application then can access the developer's credentials from the credential store and use those credentials to access Azure resources from the app.<br><br>This method has the advantage of easier setup since a developer only needs to sign in to their Azure account from Visual Studio, VS Code or the Azure CLI. The disadvantage of this approach is that the developer's account likely has more permissions than required by the application, therefore not properly replicating the permissions the app will run with in production.<br><br>[Authorize access using developer credentials](/dotnet/azure/sdk/authentication-local-development-dev-accounts?toc=/azure/storage/blobs/toc.json&bc=/azure/storage/blobs/breadcrumb/toc.json) |
68+
| Hosted in Azure | Managed identity | Apps hosted in Azure should use a **managed identity service principal**. Managed identities are designed to represent the identity of an app hosted in Azure and can only be used with Azure hosted apps.<br><br>For example, a .NET web app hosted in Azure App Service would be assigned a managed identity. The managed identity assigned to the app would then be used to authenticate the app to other Azure services.<br><br>[Authorize access from Azure-hosted apps using a managed identity](/dotnet/azure/sdk/authentication-azure-hosted-apps?toc=/azure/storage/blobs/toc.json&bc=/azure/storage/blobs/breadcrumb/toc.json) |
69+
| Hosted outside of Azure (for example, on-premises apps) | Service principal | Apps hosted outside of Azure (for example on-premises apps) that need to connect to Azure services should use an **application service principal**. An application service principal represents the identity of the app in Azure and is created through the application registration process.<br><br>For example, consider a .NET web app hosted on-premises that makes use of Azure Blob Storage. You would create an application service principal for the app using the App registration process. The `AZURE_CLIENT_ID`, `AZURE_TENANT_ID`, and `AZURE_CLIENT_SECRET` would all be stored as environment variables to be read by the application at runtime and allow the app to authenticate to Azure using the application service principal.<br><br>[Authorize access from on-premises apps using an application service principal](/dotnet/azure/sdk/authentication-on-premises-apps?toc=/azure/storage/blobs/toc.json&bc=/azure/storage/blobs/breadcrumb/toc.json) |
70+
71+
The easiest way to authorize access and connect to Blob Storage is to obtain an OAuth token by creating a [DefaultAzureCredential](/dotnet/api/azure.identity.defaultazurecredential) instance. You can then use that credential to create a [BlobServiceClient](/dotnet/api/azure.storage.blobs.blobserviceclient) object.
6172

62-
#### Authorize with an account key
73+
```csharp
74+
public static void GetBlobServiceClient(ref BlobServiceClient blobServiceClient, string accountName)
75+
{
76+
TokenCredential credential = new DefaultAzureCredential();
77+
78+
string blobUri = "https://" + accountName + ".blob.core.windows.net";
79+
80+
blobServiceClient = new BlobServiceClient(new Uri(blobUri), credential);
81+
}
82+
```
83+
84+
If you know exactly which credential type you'll use to authenticate users, you can obtain an OAuth token by using other classes in the [Azure Identity client library for .NET](/dotnet/api/overview/azure/identity-readme). These classes derive from the [TokenCredential](/dotnet/api/azure.core.tokencredential) class.
85+
86+
## [Account key](#tab/account-key)
6387

6488
Create a [StorageSharedKeyCredential](/dotnet/api/azure.storage.storagesharedkeycredential) by using the storage account name and account key. Then use that object to initialize a [BlobServiceClient](/dotnet/api/azure.storage.blobs.blobserviceclient).
6589

@@ -85,7 +109,7 @@ BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
85109

86110
For information about how to obtain account keys and best practice guidelines for properly managing and safeguarding your keys, see [Manage storage account access keys](../common/storage-account-keys-manage.md).
87111

88-
#### Authorize with a SAS token
112+
## [SAS token](#tab/sas-token)
89113

90114
Create a [Uri](/dotnet/api/system.uri) by using the blob service endpoint and SAS token. Then, create a [BlobServiceClient](/dotnet/api/azure.storage.blobs.blobserviceclient) by using the [Uri](/dotnet/api/system.uri).
91115

@@ -110,46 +134,9 @@ To generate and manage SAS tokens, see any of these articles:
110134

111135
- [Create a user delegation SAS for a container, directory, or blob with .NET](storage-blob-user-delegation-sas-create-dotnet.md)
112136

113-
#### Authorize with Azure AD
114-
115-
To authorize with Azure AD, you'll need to use a security principal. Which type of security principal you need depends on where your application runs. Use this table as a guide.
116-
117-
| Where the application runs | Security principal | Guidance |
118-
|--|--|---|
119-
| Local machine (developing and testing) | User identity or service principal | [Use the Azure Identity library to get an access token for authorization](../common/identity-library-acquire-token.md) |
120-
| Azure | Managed identity | [Authorize access to blob data with managed identities for Azure resources](authorize-managed-identity.md) |
121-
| Servers or clients outside of Azure | Service principal | [Authorize access to blob or queue data from a native or web application](../common/storage-auth-aad-app.md?toc=/azure/storage/blobs/toc.json) |
122-
123-
If you're testing on a local machine, or your application will run in Azure virtual machines (VMs), function apps, virtual machine scale sets, or in other Azure services, obtain an OAuth token by creating a [DefaultAzureCredential](/dotnet/api/azure.identity.defaultazurecredential) instance. Use that object to create a [BlobServiceClient](/dotnet/api/azure.storage.blobs.blobserviceclient).
124-
125-
```csharp
126-
public static void GetBlobServiceClient(ref BlobServiceClient blobServiceClient, string accountName)
127-
{
128-
TokenCredential credential = new DefaultAzureCredential();
129-
130-
string blobUri = "https://" + accountName + ".blob.core.windows.net";
131-
132-
blobServiceClient = new BlobServiceClient(new Uri(blobUri), credential);
133-
}
134-
```
135-
136-
If you plan to deploy the application to servers and clients that run outside of Azure, you can obtain an OAuth token by using other classes in the [Azure Identity client library for .NET](/dotnet/api/overview/azure/identity-readme) which derive from the [TokenCredential](/dotnet/api/azure.core.tokencredential) class.
137-
138-
This example creates a [ClientSecretCredential](/dotnet/api/azure.identity.clientsecretcredential) instance by using the client ID, client secret, and tenant ID. You can obtain these values when you create an app registration and service principal.
139-
140-
```csharp
141-
public static void GetBlobServiceClientAzureAD(ref BlobServiceClient blobServiceClient,
142-
string accountName, string clientID, string clientSecret, string tenantID)
143-
{
144-
145-
TokenCredential credential = new ClientSecretCredential(
146-
tenantID, clientID, clientSecret, new TokenCredentialOptions());
147-
148-
string blobUri = "https://" + accountName + ".blob.core.windows.net";
137+
---
149138

150-
blobServiceClient = new BlobServiceClient(new Uri(blobUri), credential);
151-
}
152-
```
139+
To learn more about each of these authorization mechanisms, see [Authorize access to data in Azure Storage](../common/authorize-data-access.md).
153140

154141
## Build your application
155142

@@ -165,7 +152,7 @@ The following diagram shows the relationship between these resources.
165152

166153
![Diagram of Blob storage architecture](./media/storage-blobs-introduction/blob1.png)
167154

168-
Each type of resource is represented by one or more associated .NET classes. These are the basic classes:
155+
Each type of resource is represented by one or more associated .NET classes. This table lists the basic classes with a brief description:
169156

170157
| Class | Description |
171158
|---|---|
@@ -190,7 +177,7 @@ The following guides show you how to use each of these classes to build your app
190177
| [Copy blobs](storage-blob-copy.md) | Copy a blob from one account to another account. |
191178
| [List blobs](storage-blobs-list.md) | List blobs in different ways. |
192179
| [Delete and restore](storage-blob-delete.md) | Delete blobs, and if soft-delete is enabled, restore deleted blobs. |
193-
| [Find blobs using tags](storage-blob-tags.md) | Set and retrieve tags as well as use tags to find blobs. |
180+
| [Find blobs using tags](storage-blob-tags.md) | Set and retrieve tags, and use tags to find blobs. |
194181
| [Manage properties and metadata](storage-blob-properties-metadata.md) | Get and set properties and metadata for blobs. |
195182

196183
## See also

0 commit comments

Comments
 (0)