Skip to content

Commit 0260007

Browse files
committed
Updates
1 parent 91f6138 commit 0260007

File tree

2 files changed

+43
-17
lines changed

2 files changed

+43
-17
lines changed

aspnetcore/blazor/call-web-api.md

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,20 +61,33 @@ In the app's `Program` file, call:
6161

6262
* <xref:Microsoft.Identity.Web.MicrosoftIdentityWebApiAuthenticationBuilder.EnableTokenAcquisitionToCallDownstreamApi%2A>: Enables token acquisition to call web APIs.
6363
* `AddDownstreamApi`: Adds a named downstream web service related to a specific configuration section.
64-
* <xref:Microsoft.Identity.Web.TokenCacheProviders.InMemory.InMemoryTokenCacheProviderExtension.AddInMemoryTokenCaches%2A>: Adds both the app and per-user in-memory token caches.
64+
* <xref:Microsoft.Identity.Web.TokenCacheProviders.Distributed.DistributedTokenCacheAdapterExtension.AddDistributedTokenCaches%2A>: Adds the .NET Core distributed cache based app token cache to the service collection.
65+
* <xref:Microsoft.Extensions.DependencyInjection.MemoryCacheServiceCollectionExtensions.AddDistributedMemoryCache%2A>: Adds a default implementation of <xref:Microsoft.Extensions.Caching.Distributed.IDistributedCache> that stores cache items in memory.
6566

6667
```csharp
6768
builder.Services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
6869
.AddMicrosoftIdentityWebApp(builder.Configuration.GetSection("AzureAd"))
6970
.EnableTokenAcquisitionToCallDownstreamApi()
70-
.AddDownstreamApi("DownstreamApi", builder.Configuration.GetSection("DownstreamApi"))
71-
.AddInMemoryTokenCaches();
71+
.AddDownstreamApi("DownstreamApi",
72+
builder.Configuration.GetSection("DownstreamApi"))
73+
.AddDistributedTokenCaches();
74+
75+
// Requires the 'Microsoft.Extensions.Caching.Memory' NuGet package
76+
builder.Services.AddDistributedMemoryCache();
7277
```
7378

74-
> [IMPORTANT]
75-
> In-memory token caches are created when calling <xref:Microsoft.Identity.Web.TokenCacheProviders.InMemory.InMemoryTokenCacheProviderExtension.AddInMemoryTokenCaches%2A>, but production web apps and web APIs should use distributed token caches (for example: [Redis](https://redis.io/), [Microsoft SQL Server](https://www.microsoft.com/sql-server), [Microsoft Azure Cosmos DB](https://azure.microsoft.com/products/cosmos-db)) in conjunction with a constrained memory cache.
76-
>
77-
> For more information, see [Token cache serialization: Distributed caches](/entra/msal/dotnet/how-to/token-cache-serialization?tabs=msal#distributed-caches).
79+
In-memory distributed token caches are created when calling <xref:Microsoft.Identity.Web.TokenCacheProviders.Distributed.DistributedTokenCacheAdapterExtension.AddDistributedTokenCaches%2A> to ensure that there is a base implementation available for distributed token caching.
80+
81+
Production web apps and web APIs should use a production distributed token cache (for example: [Redis](https://redis.io/), [Microsoft SQL Server](https://www.microsoft.com/sql-server), [Microsoft Azure Cosmos DB](https://azure.microsoft.com/products/cosmos-db)).
82+
83+
<xref:Microsoft.Extensions.DependencyInjection.MemoryCacheServiceCollectionExtensions.AddDistributedMemoryCache%2A> adds a default implementation of <xref:Microsoft.Extensions.Caching.Distributed.IDistributedCache> that stores cache items in memory, which is used by Microsoft Identity Web for token caching.
84+
85+
> [!NOTE]
86+
> <xref:Microsoft.Extensions.DependencyInjection.MemoryCacheServiceCollectionExtensions.AddDistributedMemoryCache%2A> requires a package reference to the [`Microsoft.Extensions.Caching.Memory` NuGet package](https://www.nuget.org/packages/Microsoft.Extensions.Caching.Memory).
87+
88+
To configure a production distributed cache provider, see <xref:performance/caching/distributed>.
89+
90+
For more information, see [Token cache serialization: Distributed caches](/entra/msal/dotnet/how-to/token-cache-serialization?tabs=msal#distributed-caches). However, the code examples shown don't apply to ASP.NET Core apps, which configure distributed caches via <xref:Microsoft.Extensions.DependencyInjection.MemoryCacheServiceCollectionExtensions.AddDistributedMemoryCache%2A>, not <xref:Microsoft.Identity.Web.TokenCacheExtensions.AddDistributedTokenCache%2A>. Don't attempt to use the code shown in a Blazor app.
7891

7992
Inject <xref:Microsoft.Identity.Abstractions.IDownstreamApi> and call <xref:Microsoft.Identity.Abstractions.IDownstreamApi.CallApiForUserAsync%2A> when calling on behalf of a user:
8093

aspnetcore/blazor/security/blazor-web-app-with-entra.md

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ builder.Services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
171171
configOptions.BaseUrl = "{BASE ADDRESS}";
172172
configOptions.Scopes = [ "{APP ID URI}/Weather.Get" ];
173173
})
174-
.AddInMemoryTokenCaches();
174+
.AddDistributedTokenCaches();
175175
```
176176

177177
Placeholders in the preceding configuration:
@@ -203,7 +203,7 @@ builder.Services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
203203
configOptions.BaseUrl = "https://localhost:7277";
204204
configOptions.Scopes = [ "api://11112222-bbbb-3333-cccc-4444dddd5555/Weather.Get" ];
205205
})
206-
.AddInMemoryTokenCaches();
206+
.AddDistributedTokenCaches();
207207
```
208208

209209
:::zone-end
@@ -381,7 +381,7 @@ builder.Services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
381381
configOptions.BaseUrl = "{BASE ADDRESS}";
382382
configOptions.Scopes = [ "{APP ID URI}/Weather.Get" ];
383383
})
384-
.AddInMemoryTokenCaches();
384+
.AddDistributedTokenCaches();
385385
```
386386

387387
Placeholders in the preceding configuration:
@@ -413,13 +413,13 @@ builder.Services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
413413
configOptions.BaseUrl = "https://localhost:7277";
414414
configOptions.Scopes = [ "api://11112222-bbbb-3333-cccc-4444dddd5555/Weather.Get" ];
415415
})
416-
.AddInMemoryTokenCaches();
416+
.AddDistributedTokenCaches();
417417
```
418418

419419
:::zone-end
420420

421421
> [!NOTE]
422-
> The preceding examples use in-memory token caches, but production apps should use distributed token caches. For more information, see the [Use distributed token caches in production](#use-distributed-token-caches-in-production) section.
422+
> The preceding examples use in-memory distributed token caches, but production apps should use a production distributed token cache provider. For more information, see the [Use a production distributed token cache provider](#use-a-production-distributed-token-cache-provider) section.
423423
424424
The callback path (`CallbackPath`) must match the redirect URI (login callback path) configured when registering the application in the Entra or Azure portal. Paths are configured in the **Authentication** blade of the app's registration. The default value of `CallbackPath` is `/signin-oidc` for a registered redirect URI of `https://localhost/signin-oidc` (a port isn't required).
425425

@@ -645,11 +645,11 @@ builder.Services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
645645
- configOptions.Scopes = [ "..." ];
646646
- })
647647
+ .AddDownstreamApi("DownstreamApi", builder.Configuration.GetSection("DownstreamApi"))
648-
.AddInMemoryTokenCaches();
648+
.AddDistributedTokenCaches();
649649
```
650650

651651
> [!NOTE]
652-
> The preceding example uses in-memory token caches, but production apps should use distributed token caches. For more information, see the [Use distributed token caches in production](#use-distributed-token-caches-in-production) section.
652+
> The preceding example uses in-memory distributed token caches, but production apps should use a production distributed token cache provider. For more information, see the [Use a production distributed token cache provider](#use-a-production-distributed-token-cache-provider) section.
653653
654654
In the `MinimalApiJwt` project, add the following app settings configuration to the `appsettings.json` file:
655655

@@ -693,11 +693,24 @@ For more information on configuration, see the following resources:
693693
* <xref:fundamentals/configuration/index>
694694
* <xref:blazor/fundamentals/configuration>
695695

696-
## Use distributed token caches in production
696+
## Use a production distributed token cache provider
697697

698-
In-memory token caches are created when calling <xref:Microsoft.Identity.Web.TokenCacheProviders.InMemory.InMemoryTokenCacheProviderExtension.AddInMemoryTokenCaches%2A>, but production web apps and web APIs should use distributed token caches (for example: [Redis](https://redis.io/), [Microsoft SQL Server](https://www.microsoft.com/sql-server), [Microsoft Azure Cosmos DB](https://azure.microsoft.com/products/cosmos-db)) in conjunction with a constrained memory cache.
698+
In-memory distributed token caches are created when calling <xref:Microsoft.Identity.Web.TokenCacheProviders.Distributed.DistributedTokenCacheAdapterExtension.AddDistributedTokenCaches%2A> to ensure that there is a base implementation available for distributed token caching.
699699

700-
For more information, see [Token cache serialization: Distributed caches](/entra/msal/dotnet/how-to/token-cache-serialization?tabs=msal#distributed-caches).
700+
Production web apps and web APIs should use a production distributed token cache (for example: [Redis](https://redis.io/), [Microsoft SQL Server](https://www.microsoft.com/sql-server), [Microsoft Azure Cosmos DB](https://azure.microsoft.com/products/cosmos-db)).
701+
702+
<xref:Microsoft.Extensions.DependencyInjection.MemoryCacheServiceCollectionExtensions.AddDistributedMemoryCache%2A> adds a default implementation of <xref:Microsoft.Extensions.Caching.Distributed.IDistributedCache> that stores cache items in memory, which is used by Microsoft Identity Web for token caching:
703+
704+
```csharp
705+
builder.Services.AddDistributedMemoryCache();
706+
```
707+
708+
> [!NOTE]
709+
> <xref:Microsoft.Extensions.DependencyInjection.MemoryCacheServiceCollectionExtensions.AddDistributedMemoryCache%2A> requires a package reference to the [`Microsoft.Extensions.Caching.Memory` NuGet package](https://www.nuget.org/packages/Microsoft.Extensions.Caching.Memory).
710+
711+
To configure a production distributed cache provider, see <xref:performance/caching/distributed>.
712+
713+
For more information, see [Token cache serialization: Distributed caches](/entra/msal/dotnet/how-to/token-cache-serialization?tabs=msal#distributed-caches). However, the code examples shown don't apply to ASP.NET Core apps, which configure distributed caches via <xref:Microsoft.Extensions.DependencyInjection.MemoryCacheServiceCollectionExtensions.AddDistributedMemoryCache%2A>, not <xref:Microsoft.Identity.Web.TokenCacheExtensions.AddDistributedTokenCache%2A>. Don't attempt to use the code shown in a Blazor app.
701714

702715
## Redirect to the home page on logout
703716

0 commit comments

Comments
 (0)