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

[Bug] Caching by MethodInfo will return same cache for different distributed cache configurations #1447

Open
2 of 8 tasks
rymeskar opened this issue Sep 16, 2021 · 5 comments
Labels

Comments

@rymeskar
Copy link

Which version of Microsoft Identity Web are you using?
Latest master

Where is the issue?

  • Web app
    • Sign-in users
    • Sign-in users and call web APIs
  • Web API
    • Protected web APIs (validating tokens)
    • Protected web APIs (validating scopes)
    • Protected web APIs call downstream web APIs
  • Token cache serialization
    • In-memory caches
    • Session caches
    • Distributed caches
  • Other (please describe)

In the helper method for applications that would like to use Id.Web and MSAL caching and do not use DI, there is a cache in order to remove redundant initialization.

This cache however has a bug of only caching by MethodInfo. Relying on equality or difference of the produced MethodInfo is problematic. For example, I showcase that same pattern as is used for Distributed Cache registration results in not adding newly configured caches. There will always be just key for the distributed cache. Here's the full repro.

@jmprieur jmprieur added the bug Something isn't working label Sep 27, 2021
@jennyf19 jennyf19 added the R9 Internal 1P label Oct 28, 2021
@jennyf19
Copy link
Collaborator

jennyf19 commented Nov 3, 2021

Updating the thread here:

so this test is the issue.

It's supposed to check that we use the same cca instance, but when checking the Distributed cache in the last line # 45, instead of the token source being cache, it's identity provider. This was happening with the Delegate & object suggestions from you repo and got the same result.

For this #1390

branch to use

cc: @rymeskar

@jmprieur
Copy link
Collaborator

Closing, as I believe, we concluded this was a non-repro

@rymeskar
Copy link
Author

rymeskar commented Feb 4, 2023

I think this is still a bug @jmprieur . I created a more relevant repro.

      [Fact]
      public void BugRepro()
      {
          var cca1 = ConfidentialClientApplicationBuilder
                         .Create(TestConstants.ClientId)
                         .WithAuthority(TestConstants.AuthorityCommonTenant)
                         .WithClientSecret(TestConstants.ClientSecret)
                         .Build();
          var cca2 = ConfidentialClientApplicationBuilder
                         .Create(TestConstants.ClientId)
                         .WithAuthority(TestConstants.AuthorityCommonTenant)
                         .WithClientSecret(TestConstants.ClientSecret)
                         .Build();
          var cca3 = ConfidentialClientApplicationBuilder
                         .Create(TestConstants.ClientId)
                         .WithAuthority(TestConstants.AuthorityCommonTenant)
                         .WithClientSecret(TestConstants.ClientSecret)
                         .Build();
          var cca4 = ConfidentialClientApplicationBuilder
                         .Create(TestConstants.ClientId)
                         .WithAuthority(TestConstants.AuthorityCommonTenant)
                         .WithClientSecret(TestConstants.ClientSecret)
                         .Build();
          var cca5 = ConfidentialClientApplicationBuilder
                         .Create(TestConstants.ClientId)
                         .WithAuthority(TestConstants.AuthorityCommonTenant)
                         .WithClientSecret(TestConstants.ClientSecret)
                         .Build();
          var cca6 = ConfidentialClientApplicationBuilder
                         .Create(TestConstants.ClientId)
                         .WithAuthority(TestConstants.AuthorityCommonTenant)
                         .WithClientSecret(TestConstants.ClientSecret)
                         .Build();
          var cca7 = ConfidentialClientApplicationBuilder
                         .Create(TestConstants.ClientId)
                         .WithAuthority(TestConstants.AuthorityCommonTenant)
                         .WithClientSecret(TestConstants.ClientSecret)
                         .Build();
          var cca8 = ConfidentialClientApplicationBuilder
              .Create(TestConstants.ClientId)
              .WithAuthority(TestConstants.AuthorityCommonTenant)
              .WithClientSecret(TestConstants.ClientSecret)
              .Build();

          cca1.AddDistributedTokenCache(services =>
          {
              count++;
              services.AddDistributedMemoryCache();
          });
          Assert.Equal(1, count); // correct increase.

          cca2.AddDistributedTokenCache(services =>
          {
              count++;
              services.AddDistributedMemoryCache();
          });
          Assert.Equal(1, count); // up for discussion whether this is correct or not because the action reference is actually different.

          cca3.AddDistributedTokenCache(AddCache);
          Assert.Equal(1, count); // this needs to be a count increase

          cca4.AddDistributedTokenCache(AddCache);
          Assert.Equal(1, count); // this should not be a count increase

          cca5.AddDistributedTokenCache(s =>
          {
              s.AddDistributedMemoryCache();
              s.AddMemoryCache();
              s.AddLogging(l => l.AddEventLog());
              count++;
          });
          Assert.Equal(1, count); // this needs to be a count increase

          cca6.AddInMemoryTokenCache(AddCache);
          Assert.Equal(2, count); // this is correct count increase

          cca7.AddInMemoryTokenCache(s =>
          {
              s.AddDistributedMemoryCache();
              s.AddMemoryCache();
              s.AddLogging(l => l.AddEventLog());
              count++;
          });
          Assert.Equal(2, count); // this needs to be a count increase

          cca8.AddInMemoryTokenCache();
          Assert.Equal(2, count); // this needs to be a count increase
      }

@bgavrilMS bgavrilMS reopened this Mar 16, 2023
@bgavrilMS
Copy link
Member

Reopening as per feedback from Karel.

@bgavrilMS
Copy link
Member

@pmaytak - can you also take a look at this please?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants