Skip to content

Commit d4ceb51

Browse files
authored
Add sample showing using service-to-service authentication to store DataProtection keys in Azure (dotnet#12826)
* Update key-storage-providers.md * Update overview.md * Update aspnetcore-1.1.md
1 parent e7e1de8 commit d4ceb51

File tree

3 files changed

+40
-17
lines changed

3 files changed

+40
-17
lines changed

aspnetcore/release-notes/aspnetcore-1.1.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ ASP.NET Core 1.1 includes the following new features:
1818
- [Cookie-based TempData provider](xref:fundamentals/app-state#tempdata)
1919
- [Azure App Service logging provider](xref:fundamentals/logging/index#azure-app-service-provider)
2020
- [Azure Key Vault configuration provider](xref:security/key-vault-configuration)
21-
- [Azure and Redis Storage Data Protection Key Repositories](xref:security/data-protection/implementation/key-storage-providers#azure-and-redis)
21+
- [Azure and Redis Storage Data Protection Key Repositories](xref:security/data-protection/implementation/key-storage-providers)
2222
- WebListener Server for Windows
2323
- [WebSockets support](xref:fundamentals/websockets)
2424

aspnetcore/security/data-protection/configuration/overview.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ When the Data Protection system is initialized, it applies [default settings](xr
1717
For these scenarios, the Data Protection system offers a rich configuration API.
1818

1919
> [!WARNING]
20-
> Similar to configuration files, the data protection key ring should be protected using appropriate permissions. You can choose to encrypt keys at rest, but this doesn't prevent attackers from creating new keys. Consequently, your app's security is impacted. The storage location configured with Data Protection should have its access limited to the app itself, similar to the way you would protect configuration files. For example, if you choose to store your key ring on disk, use file system permissions. Ensure only the identity under which your web app runs has read, write, and create access to that directory. If you use Azure Table Storage, only the web app should have the ability to read, write, or create new entries in the table store, etc.
20+
> Similar to configuration files, the data protection key ring should be protected using appropriate permissions. You can choose to encrypt keys at rest, but this doesn't prevent attackers from creating new keys. Consequently, your app's security is impacted. The storage location configured with Data Protection should have its access limited to the app itself, similar to the way you would protect configuration files. For example, if you choose to store your key ring on disk, use file system permissions. Ensure only the identity under which your web app runs has read, write, and create access to that directory. If you use Azure Blob Storage, only the web app should have the ability to read, write, or create new entries in the blob store, etc.
2121
>
2222
> The extension method [AddDataProtection](/dotnet/api/microsoft.extensions.dependencyinjection.dataprotectionservicecollectionextensions.adddataprotection) returns an [IDataProtectionBuilder](/dotnet/api/microsoft.aspnetcore.dataprotection.idataprotectionbuilder). `IDataProtectionBuilder` exposes extension methods that you can chain together to configure Data Protection options.
2323
@@ -36,7 +36,7 @@ public void ConfigureServices(IServiceCollection services)
3636
}
3737
```
3838

39-
Set the key ring storage location (for example, [PersistKeysToAzureBlobStorage](/dotnet/api/microsoft.aspnetcore.dataprotection.azuredataprotectionbuilderextensions.persistkeystoazureblobstorage)). The location must be set because calling `ProtectKeysWithAzureKeyVault` implements an [IXmlEncryptor](/dotnet/api/microsoft.aspnetcore.dataprotection.xmlencryption.ixmlencryptor) that disables automatic data protection settings, including the key ring storage location. The preceding example uses Azure Blob Storage to persist the key ring. For more information, see [Key storage providers: Azure and Redis](xref:security/data-protection/implementation/key-storage-providers#azure-and-redis). You can also persist the key ring locally with [PersistKeysToFileSystem](xref:security/data-protection/implementation/key-storage-providers#file-system).
39+
Set the key ring storage location (for example, [PersistKeysToAzureBlobStorage](/dotnet/api/microsoft.aspnetcore.dataprotection.azuredataprotectionbuilderextensions.persistkeystoazureblobstorage)). The location must be set because calling `ProtectKeysWithAzureKeyVault` implements an [IXmlEncryptor](/dotnet/api/microsoft.aspnetcore.dataprotection.xmlencryption.ixmlencryptor) that disables automatic data protection settings, including the key ring storage location. The preceding example uses Azure Blob Storage to persist the key ring. For more information, see [Key storage providers: Azure Storage](xref:security/data-protection/implementation/key-storage-providers#azure-storage). You can also persist the key ring locally with [PersistKeysToFileSystem](xref:security/data-protection/implementation/key-storage-providers#file-system).
4040

4141
The `keyIdentifier` is the key vault key identifier used for key encryption. For example, a key created in key vault named `dataprotection` in the `contosokeyvault` has the key identifier `https://contosokeyvault.vault.azure.net/keys/dataprotection/`. Provide the app with **Unwrap Key** and **Wrap Key** permissions to the key vault.
4242

aspnetcore/security/data-protection/implementation/key-storage-providers.md

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Key storage providers in ASP.NET Core
33
author: rick-anderson
44
description: Learn about key storage providers in ASP.NET Core and how to configure key storage locations.
55
ms.author: riande
6-
ms.date: 12/19/2018
6+
ms.date: 06/11/2019
77
uid: security/data-protection/implementation/key-storage-providers
88
---
99
# Key storage providers in ASP.NET Core
@@ -27,21 +27,11 @@ public void ConfigureServices(IServiceCollection services)
2727

2828
The `DirectoryInfo` can point to a directory on the local machine, or it can point to a folder on a network share. If pointing to a directory on the local machine (and the scenario is that only apps on the local machine require access to use this repository), consider using [Windows DPAPI](xref:security/data-protection/implementation/key-encryption-at-rest) (on Windows) to encrypt the keys at rest. Otherwise, consider using an [X.509 certificate](xref:security/data-protection/implementation/key-encryption-at-rest) to encrypt keys at rest.
2929

30-
## Azure and Redis
30+
## Azure Storage
3131

32-
::: moniker range=">= aspnetcore-2.2"
33-
34-
The [Microsoft.AspNetCore.DataProtection.AzureStorage](https://www.nuget.org/packages/Microsoft.AspNetCore.DataProtection.AzureStorage/) and [Microsoft.AspNetCore.DataProtection.StackExchangeRedis](https://www.nuget.org/packages/Microsoft.AspNetCore.DataProtection.StackExchangeRedis/) packages allow storing data protection keys in Azure Storage or a Redis cache. Keys can be shared across several instances of a web app. Apps can share authentication cookies or CSRF protection across multiple servers.
32+
The [Microsoft.AspNetCore.DataProtection.AzureStorage](https://www.nuget.org/packages/Microsoft.AspNetCore.DataProtection.AzureStorage/) package allows storing data protection keys in Azure Blob Storage. Keys can be shared across several instances of a web app. Apps can share authentication cookies or CSRF protection across multiple servers.
3533

36-
::: moniker-end
37-
38-
::: moniker range="< aspnetcore-2.2"
39-
40-
The [Microsoft.AspNetCore.DataProtection.AzureStorage](https://www.nuget.org/packages/Microsoft.AspNetCore.DataProtection.AzureStorage/) and [Microsoft.AspNetCore.DataProtection.Redis](https://www.nuget.org/packages/Microsoft.AspNetCore.DataProtection.Redis/) packages allow storing data protection keys in Azure Storage or a Redis cache. Keys can be shared across several instances of a web app. Apps can share authentication cookies or CSRF protection across multiple servers.
41-
42-
::: moniker-end
43-
44-
To configure the Azure Blob Storage provider, call one of the [PersistKeysToAzureBlobStorage](/dotnet/api/microsoft.aspnetcore.dataprotection.azuredataprotectionbuilderextensions.persistkeystoazureblobstorage) overloads:
34+
To configure the Azure Blob Storage provider, call one of the [PersistKeysToAzureBlobStorage](/dotnet/api/microsoft.aspnetcore.dataprotection.azuredataprotectionbuilderextensions.persistkeystoazureblobstorage) overloads.
4535

4636
```csharp
4737
public void ConfigureServices(IServiceCollection services)
@@ -51,6 +41,39 @@ public void ConfigureServices(IServiceCollection services)
5141
}
5242
```
5343

44+
If the web app is running as an Azure service, authentication tokens can be automatically created using [ Microsoft.Azure.Services.AppAuthentication](https://www.nuget.org/packages/Microsoft.Azure.Services.AppAuthentication/).
45+
46+
```csharp
47+
var tokenProvider = new AzureServiceTokenProvider();
48+
var token = await tokenProvider.GetAccessTokenAsync("https://storage.azure.com/");
49+
var credentials = new StorageCredentials(new TokenCredential(token));
50+
var storageAccount = new CloudStorageAccount(credentials, "mystorageaccount", "core.windows.net", useHttps: true);
51+
var client = storageAccount.CreateCloudBlobClient();
52+
var container = client.GetContainerReference("my-key-container");
53+
54+
// optional - provision the container automatically
55+
await container.CreateIfNotExistsAsync();
56+
57+
services.AddDataProtection()
58+
.PersistKeysToAzureBlobStorage(container, "keys.xml");
59+
```
60+
61+
See [more details about configuring service-to-service authentication.](/azure/key-vault/service-to-service-authentication)
62+
63+
## Redis
64+
65+
::: moniker range=">= aspnetcore-2.2"
66+
67+
The [Microsoft.AspNetCore.DataProtection.StackExchangeRedis](https://www.nuget.org/packages/Microsoft.AspNetCore.DataProtection.StackExchangeRedis/) package allows storing data protection keys in a Redis cache. Keys can be shared across several instances of a web app. Apps can share authentication cookies or CSRF protection across multiple servers.
68+
69+
::: moniker-end
70+
71+
::: moniker range="< aspnetcore-2.2"
72+
73+
The [Microsoft.AspNetCore.DataProtection.Redis](https://www.nuget.org/packages/Microsoft.AspNetCore.DataProtection.Redis/) package allows storing data protection keys in a Redis cache. Keys can be shared across several instances of a web app. Apps can share authentication cookies or CSRF protection across multiple servers.
74+
75+
::: moniker-end
76+
5477
::: moniker range=">= aspnetcore-2.2"
5578

5679
To configure on Redis, call one of the [PersistKeysToStackExchangeRedis](/dotnet/api/microsoft.aspnetcore.dataprotection.stackexchangeredisdataprotectionbuilderextensions.persistkeystostackexchangeredis) overloads:

0 commit comments

Comments
 (0)