Skip to content

Commit 094a9be

Browse files
author
Tiago Brenck
committed
Added steps explaining token cache on distributed environment
1 parent db7f74f commit 094a9be

File tree

1 file changed

+78
-1
lines changed

1 file changed

+78
-1
lines changed

Diff for: 6-Deploy-to-Azure/README.md

+78-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,84 @@ In the left-hand navigation pane, select the **Azure Active Directory** service,
5252

5353
Secure key management is essential to protect data in the cloud. Use [Azure Key Vault](https://azure.microsoft.com/en-ca/services/key-vault/) to encrypt keys and small secrets like passwords that use keys stored in hardware security modules (HSMs).
5454

55-
You can follow [this sample](https://github.com/Azure-Samples/app-service-msi-keyvault-dotnet) as a guide on how to use Azure KeyVault from App Service with Managed Service Identity (MSI).
55+
You can follow [this sample](https://github.com/Azure-Samples/app-service-msi-keyvault-dotnet) as a guide on how to use Azure Key Vault from App Service with Managed Service Identity (MSI).
56+
57+
## MSAL token cache on distributed environments
58+
59+
The samples in this tutorial have their token cache providers configured for apps running on a single machine. On production environment, these apps could be deployed in many machines for scalability purpose, so the token cache provider needs to be configured accordingly to this distributed architecture.
60+
61+
These are the necessary changes for each cache provider option:
62+
63+
### In memory
64+
65+
If you want to use in memory cache, use this configuration on `Startup.cs`:
66+
67+
```csharp
68+
services.AddDistributedTokenCaches()
69+
.AddDistributedMemoryCache();
70+
```
71+
72+
### Redis
73+
74+
If you want to use distributed Redis cache, use this configuration on `Startup.cs`:
75+
76+
```csharp
77+
services.AddDistributedTokenCaches()
78+
.AddStackExchangeRedisCache(options =>
79+
{
80+
options.Configuration = "<your_redis_primary_connection_string_here>";
81+
options.InstanceName = "<your_redis_instance_name>";
82+
});
83+
```
84+
85+
### SQL Server
86+
87+
There are two options for distributed SQL cache:
88+
89+
- [using .Net Core distributed cache extensions](https://docs.microsoft.com/en-us/aspnet/core/performance/caching/distributed?view=aspnetcore-2.2)
90+
- [configuring DataProtection for distributed environments](https://docs.microsoft.com/en-us/aspnet/core/security/data-protection/configuration/overview?view=aspnetcore-2.2)
91+
92+
#### If you want to use .Net Core distributed cache extensions
93+
94+
Create the cache database by running the CLI (change the parameters according to your configurations)
95+
96+
```csharp
97+
dotnet sql-cache create "<your DB connection string>" dbo <cacheTableName>
98+
```
99+
100+
Then use this configuration on `Startup.cs`:
101+
102+
```csharp
103+
services.AddDistributedTokenCaches()
104+
.AddDistributedSqlServerCache(options =>
105+
{
106+
options.ConnectionString = "<your_sql_connection_string_here>";
107+
options.SchemaName = "dbo";
108+
options.TableName = "<your_cache_table_name_here>";
109+
});
110+
```
111+
112+
#### If you want to configure `DataProtection` for distributed environments
113+
114+
You have to configure the key ring storage to a centralized location. It could be in [Azure Key Vault](https://azure.microsoft.com/services/key-vault/) or on a [UNC share](https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-dfsc/149a3039-98ce-491a-9268-2f5ddef08192).
115+
116+
> **Note**: If you change the key persistence location, the system no longer automatically encrypts keys at rest. It is recommended that you use one of the ProtectKeysWith* methods listed [in this doc](https://docs.microsoft.com/en-us/aspnet/core/security/data-protection/configuration/overview?view=aspnetcore-2.2).
117+
118+
For Azure Key Vault, configure the system with [PersistKeysToAzureBlobStorage](https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.dataprotection.azuredataprotectionbuilderextensions.persistkeystoazureblobstorage?view=aspnetcore-2.2) (also consider using [ProtectKeysWithAzureKeyVault](https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.dataprotection.azuredataprotectionbuilderextensions.protectkeyswithazurekeyvault)) in the `Startup` class:
119+
120+
```csharp
121+
services.AddDataProtection()
122+
.PersistKeysToAzureBlobStorage("<storage account connection or uri>");
123+
```
124+
125+
> **Note**: Your app must have **Unwrap Key** and **Wrap Key** permissions to the Azure Key Vault.
126+
127+
For UNC share, configure the system with [PersistKeysToFileSystem](https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.dataprotection.dataprotectionbuilderextensions.persistkeystofilesystem) (also consider using [ProtectKeysWithCertificate](https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.dataprotection.dataprotectionbuilderextensions.protectkeyswithcertificate?view=aspnetcore-2.2)) in the `Startup` class:
128+
129+
```csharp
130+
services.AddDataProtection()
131+
.PersistKeysToFileSystem(new DirectoryInfo(@"\\server\share\directory\"));
132+
```
56133

57134
## Community Help and Support
58135

0 commit comments

Comments
 (0)