Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request] Simplify the experience developer with the MSAL Token Cache. #1277

Closed
jmprieur opened this issue Jun 21, 2021 · 2 comments
Closed
Assignees
Labels
enhancement New feature or request fixed
Milestone

Comments

@jmprieur
Copy link
Collaborator

jmprieur commented Jun 21, 2021

Is your feature request related to a problem? Please describe.
Today, token caches are still complex to use with MSAL.NET. There are several pieces of documentation to read

Describe the solution you'd like
We want to have one message, which is simple and the same for every case.

addition of token caches to MSAL.NET confidential client applications in ASP.NET, or .NET Core, or .NET FW. The app.UseInMemoryTokenCaches(); and app.UseTokenCaches could be added to Microsoft.Identity.Web (probably not MSAL.NET to avoid drawing a dependency on Microsoft.Extensions.Caching.Memory.

Note that

  • These methods should not be used in ASP.NET Core, where the token cache should still be added in the ConfigureService method of the Startup.cs.

  • I don't recommend providing higher level wrappers AddRedisTokenCaches etc ..., because this would mean Ms.Id.Web would need to depend on Microsoft.Extensions.Caching.StackExchangeRedis etc ..

Proposed experience

            // (Simulates the configuration, could be a IConfiguration or anything)
            Dictionary<string, string> Configuration = new Dictionary<string, string>();

            // Create the confidential client application
            IConfidentialClientApplication app;
            app = ConfidentialClientApplicationBuilder.Create(clientId)
                // Alternatively to the certificate you can use .WithClientSecret(clientSecret)
                .WithCertificate(certDescription.Certificate)
                .WithTenantId(tenant)
                .Build();

            // In memory token cache
            app.UseInMemoryTokenCaches();

            // Or

            // In memory distributed token cache
            app.UseDistributedTokenCaches(services =>
            {
                // In net472, requires to reference Microsoft.Extensions.Caching.Memory
                services.AddDistributedMemoryCache();
            });

            // Or

            // SQL Server token cache
            app.UseDistributedTokenCaches(services =>
            {
                services.AddDistributedSqlServerCache(options =>
                {
                    // In net472, requires to reference Microsoft.Extensions.Caching.Memory

                    // Requires to reference Microsoft.Extensions.Caching.SqlServer
                    options.ConnectionString = @"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=TestCache;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False";
                    options.SchemaName = "dbo";
                    options.TableName = "TestCache";

                    // You don't want the SQL token cache to be purged before the access token has expired. Usually
                    // access tokens expire after 1 hour (but this can be changed by token lifetime policies), whereas
                    // the default sliding expiration for the distributed SQL database is 20 mins. 
                    // Use a value which is above 60 mins (or the lifetime of a token in case of longer lived tokens)
                    options.DefaultSlidingExpiration = TimeSpan.FromMinutes(90);
                });
            });

            // Or 

            // Redis token cache
            app.UseDistributedTokenCaches(services =>
            {
                // Requires to reference Microsoft.Extensions.Caching.StackExchangeRedis
                services.AddStackExchangeRedisCache(options =>
                {
                    options.Configuration = "localhost";
                    options.InstanceName = "Redis";
                });
            });

            // Or

            // Cosmos DB token cache
            app.UseDistributedTokenCaches(services =>
            {
                // Requires to reference Microsoft.Extensions.Caching.Cosmos (preview)
                services.AddCosmosCache((CosmosCacheOptions cacheOptions) =>
                {
                    cacheOptions.ContainerName = Configuration["CosmosCacheContainer"];
                    cacheOptions.DatabaseName = Configuration["CosmosCacheDatabase"];
                    cacheOptions.ClientBuilder = new CosmosClientBuilder(Configuration["CosmosConnectionString"]);
                    cacheOptions.CreateIfNotExists = true;
                });
            });

Describe alternatives you've considered
Add these to MSAL.NET directly, but this would draw un-desired dependencies as MSAL.NET needs to say small for some scenarios.

@jmprieur jmprieur added the enhancement New feature or request label Jun 21, 2021
@jmprieur jmprieur added this to the 1.14 milestone Jun 21, 2021
@jennyf19 jennyf19 added the fixed label Jun 22, 2021
@jennyf19
Copy link
Collaborator

@jennyf19
Copy link
Collaborator

Included in 1.14 release

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request fixed
Projects
None yet
Development

No branches or pull requests

2 participants