AzureAD / microsoft-identity-web

Helps creating protected web apps and web APIs with Microsoft identity platform and Azure AD B2C
MIT License
681 stars 211 forks source link

[Feature Request] Provide glue to integrate IConfidentialClientApplication and IMsalTokenCacheProvider #1448

Open rymeskar opened 3 years ago

rymeskar commented 3 years ago

It seems like you already today provide the customer the option to register the caches to service collection. You are also creating a separate caching package without the ITokenAcquisition API.

The user might take advantage of the native pattern of registrations the cache implementation to DI. But then the actual cca/cache glue code is provided only within the TokenAcquistion APIs [1,2] not on CCA extension methods.

Could you maybe add an extension to IConfidentialClientApplication method that expects IServiceProvider and another one that directly expects IMsalTokenCacheProvider so that customers can

  1. continue with their DI pattern on app initialization.
  2. not need to know about the specifics of cache integration pattern between CCA and Id.Web
jmprieur commented 3 years ago

@rymeskar, I'm a bit confused by the question. I believe we already provide extension methods to use the token cache with MSAL directly: https://docs.microsoft.com/en-us/azure/active-directory/develop/msal-net-token-cache-serialization?tabs=aspnet#distributed-caches

(in Microsoft.Identity.Web.TokenCache)

rymeskar commented 3 years ago

@jmprieur the current API re-initialize the ServiceCollection from scratch. My proposal is that the user has already registered the Microsoft.Extensions.* caches into his DI (ServiceProvider) already at app startup. IMHO Id.Web should provide a way to work with such ServiceProvider as well. Not just this from-scratch way.

jmprieur commented 3 years ago

@rymeskar : I'm still confused. Do the following work?

https://github.com/AzureAD/microsoft-identity-web/blob/af6b515e2e49a244e9de31c9feba18a6fd9467cf/src/Microsoft.Identity.Web.TokenCache/Distributed/DistributedTokenCacheAdapterExtension.cs#L18

and

https://github.com/AzureAD/microsoft-identity-web/blob/af6b515e2e49a244e9de31c9feba18a6fd9467cf/src/Microsoft.Identity.Web.TokenCache/InMemory/InMemoryTokenCacheProviderExtension.cs#L17

?

rymeskar commented 3 years ago

These are useful at startup (IServiceCollection).

The missing signature I had in mind is: public static IConfidentialClientApplication AddInMemoryTokenCache( this IConfidentialClientApplication confidentialClientApp, IServiceProvider serviceProvider)

etc.