SessionTokenCacheProviderExtension.AddSessionAppTokenCache Method

Definition

Adds an HTTP session-based application token cache to the service collection.

public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddSessionAppTokenCache (this Microsoft.Extensions.DependencyInjection.IServiceCollection services);
static member AddSessionAppTokenCache : Microsoft.Extensions.DependencyInjection.IServiceCollection -> Microsoft.Extensions.DependencyInjection.IServiceCollection
<Extension()>
Public Function AddSessionAppTokenCache (services As IServiceCollection) As IServiceCollection

Parameters

services
IServiceCollection

The services collection to add to.

Returns

The service collection.

Remarks

For this session cache to work effectively the ASP.NET Core session has to be configured properly. The latest guidance is provided at https://docs.microsoft.com/aspnet/core/fundamentals/app-state.

In the method public void ConfigureServices(IServiceCollection services) in Startup.cs, add the following:

services.AddSession(option =>
{
    option.Cookie.IsEssential = true;
});

In the method public void Configure(IApplicationBuilder app, IHostingEnvironment env) in Startup.cs, add the following:

app.UseSession(); // Before UseMvc()

Because session token caches are added with scoped lifetime, they should not be used when TokenAcquisition is also used as a singleton (for example, when using Microsoft Graph SDK).

Applies to