Skip to content

Commit 0559f3f

Browse files
authored
In-memory/production distributed caches (#544)
1 parent d7d7111 commit 0559f3f

File tree

7 files changed

+82
-18
lines changed

7 files changed

+82
-18
lines changed

9.0/BlazorWebAppEntra/BlazorWebAppEntra/BlazorWebAppEntra.csproj

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@
88

99
<ItemGroup>
1010
<ProjectReference Include="..\BlazorWebAppEntra.Client\BlazorWebAppEntra.Client.csproj" />
11-
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="9.0.4" />
12-
<PackageReference Include="Microsoft.Identity.Web" Version="3.8.3" />
13-
<PackageReference Include="Microsoft.Identity.Web.DownstreamApi" Version="3.8.3" />
11+
<PackageReference Include="Azure.Extensions.AspNetCore.DataProtection.Blobs" Version="1.5.0" />
12+
<PackageReference Include="Azure.Extensions.AspNetCore.DataProtection.Keys" Version="1.6.0" />
13+
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="9.0.5" />
14+
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="9.0.5" />
15+
<PackageReference Include="Microsoft.Identity.Web" Version="3.9.1" />
16+
<PackageReference Include="Microsoft.Identity.Web.DownstreamApi" Version="3.9.1" />
1417
</ItemGroup>
1518

1619
</Project>

9.0/BlazorWebAppEntra/BlazorWebAppEntra/Program.cs

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
using BlazorWebAppEntra.Client.Weather;
2-
using BlazorWebAppEntra.Components;
3-
using BlazorWebAppEntra.Weather;
1+
using Azure.Identity;
42
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
3+
using Microsoft.AspNetCore.DataProtection;
54
using Microsoft.AspNetCore.Mvc;
65
using Microsoft.Identity.Web;
6+
using Microsoft.Identity.Web.TokenCacheProviders.Distributed;
7+
using BlazorWebAppEntra.Client.Weather;
8+
using BlazorWebAppEntra.Components;
9+
using BlazorWebAppEntra.Weather;
710

811
var builder = WebApplication.CreateBuilder(args);
912

@@ -31,7 +34,29 @@
3134
configOptions.BaseUrl = "{BASE URL}";
3235
configOptions.Scopes = [ "{APP ID URI}/Weather.Get" ];
3336
})
34-
.AddInMemoryTokenCaches();
37+
.AddDistributedTokenCaches();
38+
39+
builder.Services.AddDistributedMemoryCache();
40+
41+
builder.Services.Configure<MsalDistributedTokenCacheAdapterOptions>(
42+
options =>
43+
{
44+
// Disable L1 Cache default: false
45+
//options.DisableL1Cache = false;
46+
47+
// L1 Cache Size Limit default: 500 MB
48+
//options.L1CacheOptions.SizeLimit = 500 * 1024 * 1024;
49+
50+
// Encrypt tokens at rest default: false
51+
options.Encrypt = true;
52+
53+
// Sliding Expiration default: 1 hour
54+
//options.SlidingExpiration = TimeSpan.FromHours(1);
55+
});
56+
57+
builder.Services.AddDataProtection()
58+
.PersistKeysToAzureBlobStorage(new Uri("{BLOB URI WITH SAS TOKEN}"))
59+
.ProtectKeysWithAzureKeyVault(new Uri("{KEY IDENTIFIER}"), new DefaultAzureCredential());
3560

3661
builder.Services.AddAuthorization();
3762

9.0/BlazorWebAppEntra/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
# `BlazorWebAppEntra`
22

33
Sample app to accompany [Secure an ASP.NET Core Blazor Web App with Microsoft Entra ID](https://learn.microsoft.com/aspnet/core/blazor/security/blazor-web-app-with-entra?pivots=non-bff-pattern).
4+
5+
## Use a production distributed token cache provider
6+
7+
The sample app uses in-memory distributed token caches, but a production distributed token cache provider is recommended for production apps. For more information, see [Use a production distributed token cache provider](https://learn.microsoft.com/aspnet/core/blazor/security/blazor-web-app-with-entra?pivots=non-bff-pattern#use-a-production-distributed-token-cache-provider).

9.0/BlazorWebAppEntraBff/BlazorWebAppEntra.Client/BlazorWebAppEntra.Client.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
</PropertyGroup>
1010

1111
<ItemGroup>
12-
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="9.0.4" />
13-
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Authentication" Version="9.0.4" />
14-
<PackageReference Include="Microsoft.Extensions.Http" Version="9.0.4" />
12+
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="9.0.5" />
13+
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Authentication" Version="9.0.5" />
14+
<PackageReference Include="Microsoft.Extensions.Http" Version="9.0.5" />
1515
</ItemGroup>
1616

1717
</Project>

9.0/BlazorWebAppEntraBff/BlazorWebAppEntra/BlazorWebAppEntra.csproj

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@
99
<ItemGroup>
1010
<ProjectReference Include="..\Aspire\Aspire.ServiceDefaults\Aspire.ServiceDefaults.csproj" />
1111
<ProjectReference Include="..\BlazorWebAppEntra.Client\BlazorWebAppEntra.Client.csproj" />
12-
<PackageReference Include="Microsoft.Identity.Web" Version="3.8.3" />
13-
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="9.0.4" />
14-
<PackageReference Include="Microsoft.Extensions.ServiceDiscovery.Yarp" Version="9.2.0" />
15-
<PackageReference Include="Microsoft.Identity.Web.DownstreamApi" Version="3.8.3" />
12+
<PackageReference Include="Azure.Extensions.AspNetCore.DataProtection.Blobs" Version="1.5.0" />
13+
<PackageReference Include="Azure.Extensions.AspNetCore.DataProtection.Keys" Version="1.6.0" />
14+
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="9.0.5" />
15+
<PackageReference Include="Microsoft.Identity.Web" Version="3.9.1" />
16+
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="9.0.5" />
17+
<PackageReference Include="Microsoft.Extensions.ServiceDiscovery.Yarp" Version="9.3.0" />
18+
<PackageReference Include="Microsoft.Identity.Web.DownstreamApi" Version="3.9.1" />
1619
</ItemGroup>
1720

1821
</Project>

9.0/BlazorWebAppEntraBff/BlazorWebAppEntra/Program.cs

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
using Azure.Identity;
2+
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
3+
using Microsoft.AspNetCore.DataProtection;
4+
using Microsoft.Identity.Web;
5+
using Microsoft.Identity.Web.TokenCacheProviders.Distributed;
6+
using Microsoft.IdentityModel.Protocols.OpenIdConnect;
17
using Yarp.ReverseProxy.Transforms;
28
using BlazorWebAppEntra;
39
using BlazorWebAppEntra.Client.Weather;
410
using BlazorWebAppEntra.Components;
5-
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
6-
using Microsoft.Identity.Web;
7-
using Microsoft.IdentityModel.Protocols.OpenIdConnect;
811

912
var builder = WebApplication.CreateBuilder(args);
1013

@@ -28,7 +31,29 @@
2831
configOptions.BaseUrl = "{BASE URL}";
2932
configOptions.Scopes = [ "{APP ID URI}/Weather.Get" ];
3033
})
31-
.AddInMemoryTokenCaches();
34+
.AddDistributedTokenCaches();
35+
36+
builder.Services.AddDistributedMemoryCache();
37+
38+
builder.Services.Configure<MsalDistributedTokenCacheAdapterOptions>(
39+
options =>
40+
{
41+
// Disable L1 Cache default: false
42+
//options.DisableL1Cache = false;
43+
44+
// L1 Cache Size Limit default: 500 MB
45+
//options.L1CacheOptions.SizeLimit = 500 * 1024 * 1024;
46+
47+
// Encrypt tokens at rest default: false
48+
options.Encrypt = true;
49+
50+
// Sliding Expiration default: 1 hour
51+
//options.SlidingExpiration = TimeSpan.FromHours(1);
52+
});
53+
54+
builder.Services.AddDataProtection()
55+
.PersistKeysToAzureBlobStorage(new Uri("{BLOB URI WITH SAS TOKEN}"))
56+
.ProtectKeysWithAzureKeyVault(new Uri("{KEY IDENTIFIER}"), new DefaultAzureCredential());
3257

3358
builder.Services.AddOptions<OpenIdConnectOptions>(OpenIdConnectDefaults.AuthenticationScheme).Configure(oidcOptions =>
3459
{

9.0/BlazorWebAppEntraBff/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ If you need to open an issue that pertains to the coding of the sample app, open
1717

1818
Configure the solution following the guidance in [Secure an ASP.NET Core Blazor Web App with Microsoft Entra ID (BFF pattern)](https://learn.microsoft.com/aspnet/core/blazor/security/blazor-web-app-with-entra?pivots=bff-pattern).
1919

20+
## Use a production distributed token cache provider
21+
22+
The sample app uses in-memory distributed token caches, but a production distributed token cache provider is recommended for production apps. For more information, see [Use a production distributed token cache provider](https://learn.microsoft.com/aspnet/core/blazor/security/blazor-web-app-with-entra?pivots=non-bff-pattern#use-a-production-distributed-token-cache-provider).
23+
2024
## Run the sample
2125

2226
### Visual Studio

0 commit comments

Comments
 (0)