-
Notifications
You must be signed in to change notification settings - Fork 6
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
[MCC-732241] Fix caching with authentication when using utility extension method #71
Conversation
@@ -13,15 +13,15 @@ namespace Medidata.MAuth.Core | |||
internal class MAuthAuthenticator | |||
{ | |||
private readonly MAuthOptionsBase _options; | |||
private readonly IMemoryCache _cache = new MemoryCache(new MemoryCacheOptions()); | |||
private readonly IMemoryCache _cache; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why don't we declare this as
private static readonly IMemoryCache _cache = new MemoryCache(new MemoryCacheOptions());
here instead of in UtilityExtensions
?
Having a variable in an extension method class feels a bit weird.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, that looks like a better solution! Thanks, let me update the code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry to unresolve this, can we have a test for this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I reverted back to my original solution because having the static cache in the MauthAuthenticator
will screw up the unit tests.
Should we take a look at |
@hkurniawan-mdsol This only applies for cases where the relevant framework extension does not exist (i.e. where the utility extension must be used). So far I only know about PB using this utility so I don't think this is an urgent patch. |
Okay cool. If we're sure that only PB is affected then we're fine then. |
Just a friendly reminder: this repository is public. |
Okay maybe we do need to urge the public to update then. |
@lschreck-mdsol : I see version is bumped, but there is no update on changelog. Missed that ? Also, this unit test is failing too. |
Can we have a preview version released for testing? |
Background
Although MAuth provides a variety of extensions to use with external applications (ASP.NET Core middleware, ASP.NET WebAPI, OWIN etc.) there are cases when authentication is required in a way that's not supported by the library. For those cases the library provides utility extension methods to call externally (like the
UtilityExtensions.Authenticate()
method).The Issue
Under the hood the library is using an internal class called
MAuthAuthenticator
which sets up its own memory cache. As long as this class is used, caching will work. The utility method is using this class.The issue is that the above mentioned utility method has to instantiate this class for every call (as it's a static method therefore its options will be passed with every call), therefore a new memory cache will be also instantiated for every call effectively disabling the caching.
The Solution
For the
MAuthAuthenticator
instances, we are providing an optional constructor argument to set the cache externally. This will enable the utility extension to reuse its own cache therefore it won't recreate again and agin.@mdsol/libraries_dotnet @mdsol/architecture-enablement Please review and merge - thanks!