-
Notifications
You must be signed in to change notification settings - Fork 218
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
add cache extensions #1278
Merged
Merged
add cache extensions #1278
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
174 changes: 174 additions & 0 deletions
174
src/Microsoft.Identity.Web/TokenCacheProviders/TokenCacheExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
using Microsoft.Identity.Client; | ||
using Microsoft.Identity.Web.TokenCacheProviders; | ||
using Microsoft.Identity.Web.TokenCacheProviders.Distributed; | ||
using Microsoft.Identity.Web.TokenCacheProviders.InMemory; | ||
|
||
namespace Microsoft.Identity.Web | ||
{ | ||
/// <summary> | ||
/// Extension methods to expose a simplified developer experience for | ||
/// adding token caches to MSAL.NET confidential client applications | ||
/// in ASP.NET, or .NET Core, or .NET FW. | ||
/// </summary> | ||
public static class TokenCacheExtensions | ||
{ | ||
/// <summary> | ||
/// Use a token cache and choose the serialization part by adding it to | ||
/// the services collection and configuring its options. | ||
/// </summary> | ||
/// <returns>The confidential client application.</returns> | ||
/// <param name="confidentialClientApp">Confidential client application.</param> | ||
/// <param name="initializeCaches">Action that you'll use to add a cache serialization | ||
/// to the service collection passed as an argument.</param> | ||
/// <returns>The application for chaining.</returns> | ||
/// <example> | ||
/// | ||
/// The following code adds a distributed in-memory token cache. | ||
/// | ||
/// <code> | ||
/// app.AddTokenCaches(services => | ||
/// { | ||
/// // In memory distributed token cache | ||
/// // In net472, requires to reference Microsoft.Extensions.Caching.Memory | ||
/// services.AddDistributedTokenCaches(); | ||
/// services.AddDistributedMemoryCache(); | ||
/// }); | ||
/// </code> | ||
/// | ||
/// The following code adds a token cache based on REDIS and initializes | ||
/// its configuration. | ||
/// | ||
/// <code> | ||
/// app.AddTokenCaches(services => | ||
/// { | ||
/// services.AddDistributedTokenCaches(); | ||
/// // Redis token cache | ||
/// // Requires to reference Microsoft.Extensions.Caching.StackExchangeRedis | ||
/// services.AddStackExchangeRedisCache(options => | ||
/// { | ||
/// options.Configuration = "localhost"; | ||
/// options.InstanceName = "Redis"; | ||
/// }); | ||
/// }); | ||
/// </code> | ||
/// If using distributed token caches, use AddDistributedTokenCaches. | ||
/// </example> | ||
/// <remarks>Don't use this method in ASP.NET Core. Just add use the ConfigureServices method | ||
/// instead.</remarks> | ||
internal static IConfidentialClientApplication AddTokenCaches( | ||
this IConfidentialClientApplication confidentialClientApp, | ||
Action<IServiceCollection> initializeCaches) | ||
{ | ||
if (confidentialClientApp is null) | ||
{ | ||
throw new ArgumentNullException(nameof(confidentialClientApp)); | ||
} | ||
|
||
if (initializeCaches is null) | ||
{ | ||
throw new ArgumentNullException(nameof(initializeCaches)); | ||
} | ||
|
||
IHostBuilder hostBuilder = Host.CreateDefaultBuilder() | ||
.ConfigureLogging(logger => { }) | ||
.ConfigureServices(services => | ||
{ | ||
initializeCaches(services); | ||
}); | ||
|
||
IServiceProvider serviceProvider = hostBuilder.Build().Services; | ||
IMsalTokenCacheProvider msalTokenCacheProvider = serviceProvider.GetRequiredService<IMsalTokenCacheProvider>(); | ||
msalTokenCacheProvider.Initialize(confidentialClientApp.UserTokenCache); | ||
msalTokenCacheProvider.Initialize(confidentialClientApp.AppTokenCache); | ||
return confidentialClientApp; | ||
} | ||
|
||
/// <summary> | ||
/// Add an in-memory well partitioned token cache to MSAL.NET confidential client | ||
/// application. Don't use this method in ASP.NET Core: rather use: | ||
/// <code>services.AddInMemoryTokenCaches()</code> in ConfigureServices. | ||
/// </summary> | ||
/// <param name="confidentialClientApp">Confidential client application.</param> | ||
/// <returns>The application for chaining.</returns> | ||
/// <example> | ||
/// | ||
/// The following code adds an in-memory token cache. | ||
/// | ||
/// <code> | ||
/// app.AddInMemoryTokenCaches(); | ||
/// </code> | ||
/// | ||
/// </example> | ||
/// <remarks>Don't use this method in ASP.NET Core. Just add use the ConfigureServices method | ||
/// instead.</remarks> | ||
public static IConfidentialClientApplication AddInMemoryTokenCaches( | ||
this IConfidentialClientApplication confidentialClientApp) | ||
{ | ||
if (confidentialClientApp is null) | ||
{ | ||
throw new ArgumentNullException(nameof(confidentialClientApp)); | ||
} | ||
|
||
confidentialClientApp.AddTokenCaches(services => | ||
{ | ||
services.AddInMemoryTokenCaches(); | ||
}); | ||
return confidentialClientApp; | ||
} | ||
|
||
/// <summary> | ||
/// Add a distributed token cache. | ||
/// </summary> | ||
/// <param name="confidentialClientApp">Confidential client application.</param> | ||
/// <param name="initializeDistributedCache">Action taking a <see cref="IServiceCollection"/> | ||
/// and by which you initialize your distributed cache.</param> | ||
/// <returns>The application for chaining.</returns> | ||
/// <example> | ||
/// The following code adds a token cache based on REDIS and initializes | ||
/// its configuration. | ||
/// | ||
/// <code> | ||
/// app.AddDistributedTokenCaches(services => | ||
/// { | ||
/// // Redis token cache | ||
/// // Requires to reference Microsoft.Extensions.Caching.StackExchangeRedis | ||
/// services.AddStackExchangeRedisCache(options => | ||
/// { | ||
/// options.Configuration = "localhost"; | ||
/// options.InstanceName = "Redis"; | ||
/// }); | ||
/// }); | ||
/// </code> | ||
/// | ||
/// </example> | ||
/// <remarks>Don't use this method in ASP.NET Core. Just add use the ConfigureServices method | ||
/// instead.</remarks> | ||
public static IConfidentialClientApplication AddDistributedTokenCaches( | ||
this IConfidentialClientApplication confidentialClientApp, | ||
Action<IServiceCollection> initializeDistributedCache) | ||
{ | ||
if (confidentialClientApp is null) | ||
{ | ||
throw new ArgumentNullException(nameof(confidentialClientApp)); | ||
} | ||
|
||
if (initializeDistributedCache is null) | ||
{ | ||
throw new ArgumentNullException(nameof(initializeDistributedCache)); | ||
} | ||
|
||
confidentialClientApp.AddTokenCaches(services => | ||
{ | ||
services.AddDistributedTokenCaches(); | ||
initializeDistributedCache(services); | ||
}); | ||
return confidentialClientApp; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Identity.Client; | ||
using Microsoft.Identity.Web.Test.Common; | ||
using Xunit; | ||
|
||
namespace Microsoft.Identity.Web.Test | ||
{ | ||
public class CacheExtensionsTests | ||
{ | ||
private IConfidentialClientApplication _confidentialApp; | ||
|
||
[Fact] | ||
public void InMemoryCacheExtensionsTests() | ||
{ | ||
CreateCca(); | ||
_confidentialApp.AddInMemoryTokenCaches(); | ||
|
||
Assert.NotNull(_confidentialApp.UserTokenCache); | ||
Assert.NotNull(_confidentialApp.AppTokenCache); | ||
} | ||
|
||
[Fact] | ||
public void InMemoryCacheExtensions_NoCca_ThrowsException_Tests() | ||
{ | ||
var ex = Assert.Throws<ArgumentNullException>(() => _confidentialApp.AddInMemoryTokenCaches()); | ||
|
||
Assert.Equal("confidentialClientApp", ex.ParamName); | ||
} | ||
|
||
[Fact] | ||
public void DistributedCacheExtensionsTests() | ||
{ | ||
CreateCca(); | ||
_confidentialApp.AddDistributedTokenCaches(services => | ||
{ | ||
services.AddDistributedMemoryCache(); | ||
}); | ||
|
||
Assert.NotNull(_confidentialApp.UserTokenCache); | ||
Assert.NotNull(_confidentialApp.AppTokenCache); | ||
} | ||
|
||
[Fact] | ||
public void DistributedCacheExtensions_NoCca_ThrowsException_Tests() | ||
{ | ||
var ex = Assert.Throws<ArgumentNullException>(() => _confidentialApp.AddDistributedTokenCaches(services => | ||
{ | ||
services.AddDistributedMemoryCache(); | ||
})); | ||
|
||
Assert.Equal("confidentialClientApp", ex.ParamName); | ||
} | ||
|
||
[Fact] | ||
public void DistributedCacheExtensions_NoService_ThrowsException_Tests() | ||
{ | ||
CreateCca(); | ||
|
||
var ex = Assert.Throws<ArgumentNullException>(() => _confidentialApp.AddDistributedTokenCaches(null)); | ||
|
||
Assert.Equal("initializeDistributedCache", ex.ParamName); | ||
} | ||
|
||
private void CreateCca() | ||
{ | ||
if (_confidentialApp == null) | ||
{ | ||
_confidentialApp = ConfidentialClientApplicationBuilder | ||
.Create(TestConstants.ClientId) | ||
.WithAuthority(TestConstants.AuthorityCommonTenant) | ||
.WithClientSecret(TestConstants.ClientSecret) | ||
.Build(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Maybe add a 'see also' to recommend AddDistributedTokenCaches?
If you are using distributed token caches, is recommanded, or something like that? #Resolved