Browse Source

Backport the services.AddOpenIddict() extension changes to OpenIddict 1.x

pull/570/head
Kévin Chalet 8 years ago
parent
commit
d18432fd1d
  1. 2
      src/OpenIddict.Core/OpenIddict.Core.csproj
  2. 2
      src/OpenIddict.Core/OpenIddictExtensions.cs
  3. 12
      src/OpenIddict/OpenIddictExtensions.cs
  4. 19
      test/OpenIddict.Tests/OpenIddictExtensionsTests.cs

2
src/OpenIddict.Core/OpenIddict.Core.csproj

@ -19,7 +19,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="CryptoHelper.StrongName" Version="$(CryptoHelperVersion)" /> <PackageReference Include="CryptoHelper.StrongName" Version="$(CryptoHelperVersion)" />
<PackageReference Include="JetBrains.Annotations" Version="$(JetBrainsVersion)" PrivateAssets="All" /> <PackageReference Include="JetBrains.Annotations" Version="$(JetBrainsVersion)" PrivateAssets="All" />
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="$(AspNetCoreVersion)" /> <PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="$(AspNetCoreVersion)" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="$(AspNetCoreVersion)" /> <PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="$(AspNetCoreVersion)" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="$(AspNetCoreVersion)" /> <PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="$(AspNetCoreVersion)" />
<PackageReference Include="Microsoft.Extensions.Options" Version="$(AspNetCoreVersion)" /> <PackageReference Include="Microsoft.Extensions.Options" Version="$(AspNetCoreVersion)" />

2
src/OpenIddict.Core/OpenIddictExtensions.cs

@ -64,6 +64,8 @@ namespace Microsoft.Extensions.DependencyInjection
throw new ArgumentNullException(nameof(services)); throw new ArgumentNullException(nameof(services));
} }
services.AddDistributedMemoryCache();
services.AddMemoryCache();
services.AddOptions(); services.AddOptions();
var builder = new OpenIddictBuilder(services) var builder = new OpenIddictBuilder(services)

12
src/OpenIddict/OpenIddictExtensions.cs

@ -65,17 +65,11 @@ namespace Microsoft.AspNetCore.Builder
/* TToken: */ builder.TokenType)); /* TToken: */ builder.TokenType));
} }
// When no distributed cache has been registered in the options, // When no distributed cache has been registered in the options, use the
// try to resolve it from the dependency injection container. // global instance registered in the dependency injection container.
if (options.Cache == null) if (options.Cache == null)
{ {
options.Cache = app.ApplicationServices.GetService<IDistributedCache>(); options.Cache = app.ApplicationServices.GetRequiredService<IDistributedCache>();
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.");
}
} }
// If OpenIddict was configured to use reference tokens, replace the default access tokens/ // If OpenIddict was configured to use reference tokens, replace the default access tokens/

19
test/OpenIddict.Tests/OpenIddictExtensionsTests.cs

@ -12,6 +12,7 @@ using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Builder.Internal; using Microsoft.AspNetCore.Builder.Internal;
using Microsoft.AspNetCore.DataProtection; using Microsoft.AspNetCore.DataProtection;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using Microsoft.IdentityModel.Tokens; using Microsoft.IdentityModel.Tokens;
@ -36,24 +37,6 @@ namespace OpenIddict.Tests
"Make sure 'services.AddOpenIddict()' is correctly called from 'ConfigureServices()'.", exception.Message); "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<InvalidOperationException>(() => 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] [Fact]
public void UseOpenIddict_ThrowsAnExceptionWhenNoFlowIsEnabled() public void UseOpenIddict_ThrowsAnExceptionWhenNoFlowIsEnabled()
{ {

Loading…
Cancel
Save