diff --git a/src/OpenIddict.Core/OpenIddict.Core.csproj b/src/OpenIddict.Core/OpenIddict.Core.csproj index 0ce5415d..da7fceca 100644 --- a/src/OpenIddict.Core/OpenIddict.Core.csproj +++ b/src/OpenIddict.Core/OpenIddict.Core.csproj @@ -19,7 +19,7 @@ - + diff --git a/src/OpenIddict.Core/OpenIddictExtensions.cs b/src/OpenIddict.Core/OpenIddictExtensions.cs index 3ed52d03..84fdf251 100644 --- a/src/OpenIddict.Core/OpenIddictExtensions.cs +++ b/src/OpenIddict.Core/OpenIddictExtensions.cs @@ -64,6 +64,8 @@ namespace Microsoft.Extensions.DependencyInjection throw new ArgumentNullException(nameof(services)); } + services.AddDistributedMemoryCache(); + services.AddMemoryCache(); services.AddOptions(); var builder = new OpenIddictBuilder(services) diff --git a/src/OpenIddict/OpenIddictExtensions.cs b/src/OpenIddict/OpenIddictExtensions.cs index a2586eb9..f2607e21 100644 --- a/src/OpenIddict/OpenIddictExtensions.cs +++ b/src/OpenIddict/OpenIddictExtensions.cs @@ -65,17 +65,11 @@ namespace Microsoft.AspNetCore.Builder /* TToken: */ builder.TokenType)); } - // When no distributed cache has been registered in the options, - // try to resolve it from the dependency injection container. + // When no distributed cache has been registered in the options, use the + // global instance registered in the dependency injection container. if (options.Cache == null) { - options.Cache = app.ApplicationServices.GetService(); - - if (options.EnableRequestCaching && options.Cache == null) - { - throw new InvalidOperationException("A distributed cache implementation must be registered in the OpenIddict options " + - "or in the dependency injection container when enabling request caching support."); - } + options.Cache = app.ApplicationServices.GetRequiredService(); } // If OpenIddict was configured to use reference tokens, replace the default access tokens/ diff --git a/test/OpenIddict.Tests/OpenIddictExtensionsTests.cs b/test/OpenIddict.Tests/OpenIddictExtensionsTests.cs index 7ab1d81f..8259ffdd 100644 --- a/test/OpenIddict.Tests/OpenIddictExtensionsTests.cs +++ b/test/OpenIddict.Tests/OpenIddictExtensionsTests.cs @@ -12,6 +12,7 @@ using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder.Internal; using Microsoft.AspNetCore.DataProtection; using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.Caching.Distributed; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; using Microsoft.IdentityModel.Tokens; @@ -36,24 +37,6 @@ namespace OpenIddict.Tests "Make sure 'services.AddOpenIddict()' is correctly called from 'ConfigureServices()'.", exception.Message); } - [Fact] - public void UseOpenIddict_ThrowsAnExceptionWhenNoDistributedCacheIsRegisteredIfRequestCachingIsEnabled() - { - // Arrange - var services = new ServiceCollection(); - - services.AddOpenIddict() - .EnableRequestCaching(); - - var builder = new ApplicationBuilder(services.BuildServiceProvider()); - - // Act and assert - var exception = Assert.Throws(() => builder.UseOpenIddict()); - - Assert.Equal("A distributed cache implementation must be registered in the OpenIddict options " + - "or in the dependency injection container when enabling request caching support.", exception.Message); - } - [Fact] public void UseOpenIddict_ThrowsAnExceptionWhenNoFlowIsEnabled() {