Skip to content

Commit f35564b

Browse files
authored
Move DataProtection and Extensions.Identity to netstandard2.0/2.1 (#11008)
1 parent 655c29b commit f35564b

File tree

48 files changed

+896
-76
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+896
-76
lines changed

eng/SharedFramework.External.props

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@
9191
This references are part of Microsoft.NETCore.App, so are listed here as references to be used during compilation only.
9292
-->
9393
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0' or '$(TargetFramework)' == 'netstandard2.1'">
94+
<_CompilationOnlyReference Include="Microsoft.Win32.Registry" />
95+
<_CompilationOnlyReference Include="System.Security.Cryptography.Cng" />
96+
<_CompilationOnlyReference Include="System.Security.Principal.Windows" />
9497
<_CompilationOnlyReference Include="System.Buffers" />
9598
<_CompilationOnlyReference Include="System.ComponentModel.Annotations" />
9699
<_CompilationOnlyReference Include="System.Runtime.CompilerServices.Unsafe" />

eng/SharedFramework.Local.props

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,15 @@
88
<Project>
99
<ItemGroup>
1010
<!-- These assemblies are available as both a NuGet package and in the shared framework -->
11+
<AspNetCoreAppReferenceAndPackage Include="Microsoft.AspNetCore.DataProtection.Abstractions" />
12+
<AspNetCoreAppReferenceAndPackage Include="Microsoft.AspNetCore.Cryptography.Internal" />
13+
<AspNetCoreAppReferenceAndPackage Include="Microsoft.AspNetCore.Cryptography.KeyDerivation" />
14+
<AspNetCoreAppReferenceAndPackage Include="Microsoft.AspNetCore.DataProtection" />
15+
<AspNetCoreAppReferenceAndPackage Include="Microsoft.AspNetCore.DataProtection.Extensions" />
1116
<AspNetCoreAppReferenceAndPackage Include="Microsoft.AspNetCore.Http.Features" />
1217
<AspNetCoreAppReferenceAndPackage Include="Microsoft.AspNetCore.Metadata" />
18+
<AspNetCoreAppReferenceAndPackage Include="Microsoft.Extensions.Identity.Core" />
19+
<AspNetCoreAppReferenceAndPackage Include="Microsoft.Extensions.Identity.Stores" />
1320
<AspNetCoreAppReferenceAndPackage Include="Microsoft.AspNetCore.Connections.Abstractions" />
1421
<AspNetCoreAppReferenceAndPackage Include="Microsoft.AspNetCore.Authorization" />
1522
<AspNetCoreAppReferenceAndPackage Include="Microsoft.AspNetCore.Http.Connections.Common" />
@@ -20,11 +27,6 @@
2027

2128
<!-- These assemblies are only in the shared framework -->
2229
<AspNetCoreAppReference Include="Microsoft.AspNetCore" />
23-
<AspNetCoreAppReference Include="Microsoft.AspNetCore.DataProtection.Abstractions" />
24-
<AspNetCoreAppReference Include="Microsoft.AspNetCore.Cryptography.Internal" />
25-
<AspNetCoreAppReference Include="Microsoft.AspNetCore.Cryptography.KeyDerivation" />
26-
<AspNetCoreAppReference Include="Microsoft.AspNetCore.DataProtection" />
27-
<AspNetCoreAppReference Include="Microsoft.AspNetCore.DataProtection.Extensions" />
2830
<AspNetCoreAppReference Include="Microsoft.AspNetCore.Antiforgery" />
2931
<AspNetCoreAppReference Include="Microsoft.AspNetCore.Hosting.Abstractions" />
3032
<AspNetCoreAppReference Include="Microsoft.AspNetCore.Hosting" />
@@ -40,8 +42,6 @@
4042
<AspNetCoreAppReference Include="Microsoft.AspNetCore.WebUtilities" />
4143
<AspNetCoreAppReference Include="Microsoft.AspNetCore.Html.Abstractions" />
4244
<AspNetCoreAppReference Include="Microsoft.AspNetCore.Identity" />
43-
<AspNetCoreAppReference Include="Microsoft.Extensions.Identity.Core" />
44-
<AspNetCoreAppReference Include="Microsoft.Extensions.Identity.Stores" />
4545
<AspNetCoreAppReference Include="Microsoft.AspNetCore.Server.HttpSys" />
4646
<AspNetCoreAppReference Include="Microsoft.AspNetCore.Server.IISIntegration" />
4747
<AspNetCoreAppReference Include="Microsoft.AspNetCore.Server.IIS" />
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<!-- This file is automatically generated. -->
22
<Project Sdk="Microsoft.NET.Sdk">
33
<PropertyGroup>
4-
<TargetFrameworks>netcoreapp3.0</TargetFrameworks>
4+
<TargetFrameworks>netstandard2.0</TargetFrameworks>
55
</PropertyGroup>
6-
<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp3.0'">
7-
<Compile Include="Microsoft.AspNetCore.DataProtection.Abstractions.netcoreapp3.0.cs" />
6+
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
7+
<Compile Include="Microsoft.AspNetCore.DataProtection.Abstractions.netstandard2.0.cs" />
88

99
</ItemGroup>
1010
</Project>

src/DataProtection/Abstractions/src/Microsoft.AspNetCore.DataProtection.Abstractions.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
Commonly used types:
66
Microsoft.AspNetCore.DataProtection.IDataProtectionProvider
77
Microsoft.AspNetCore.DataProtection.IDataProtector</Description>
8-
<TargetFramework>netcoreapp3.0</TargetFramework>
8+
<TargetFramework>netstandard2.0</TargetFramework>
99
<IsAspNetCoreApp>true</IsAspNetCoreApp>
10+
<IsShippingPackage>true</IsShippingPackage>
1011
<GenerateDocumentationFile>true</GenerateDocumentationFile>
1112
<PackageTags>aspnetcore;dataprotection</PackageTags>
1213
</PropertyGroup>

src/DataProtection/AzureKeyVault/ref/Microsoft.AspNetCore.DataProtection.AzureKeyVault.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<!-- This file is automatically generated. -->
22
<Project Sdk="Microsoft.NET.Sdk">
33
<PropertyGroup>
4-
<TargetFrameworks>netcoreapp3.0</TargetFrameworks>
4+
<TargetFrameworks>netstandard2.0</TargetFrameworks>
55
</PropertyGroup>
6-
<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp3.0'">
7-
<Compile Include="Microsoft.AspNetCore.DataProtection.AzureKeyVault.netcoreapp3.0.cs" />
6+
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
7+
<Compile Include="Microsoft.AspNetCore.DataProtection.AzureKeyVault.netstandard2.0.cs" />
88
<Reference Include="Microsoft.AspNetCore.DataProtection" />
99
<Reference Include="Microsoft.Azure.KeyVault" />
1010
<Reference Include="Microsoft.IdentityModel.Clients.ActiveDirectory" />

src/DataProtection/AzureKeyVault/src/Microsoft.AspNetCore.DataProtection.AzureKeyVault.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<Description>Microsoft Azure KeyVault key encryption support.</Description>
5-
<TargetFramework>netcoreapp3.0</TargetFramework>
5+
<TargetFramework>netstandard2.0</TargetFramework>
66
<GenerateDocumentationFile>true</GenerateDocumentationFile>
77
<PackageTags>aspnetcore;dataprotection;azure;keyvault</PackageTags>
88
<IsShippingPackage>true</IsShippingPackage>

src/DataProtection/AzureStorage/ref/Microsoft.AspNetCore.DataProtection.AzureStorage.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<!-- This file is automatically generated. -->
22
<Project Sdk="Microsoft.NET.Sdk">
33
<PropertyGroup>
4-
<TargetFrameworks>netcoreapp3.0</TargetFrameworks>
4+
<TargetFrameworks>netstandard2.0</TargetFrameworks>
55
</PropertyGroup>
6-
<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp3.0'">
7-
<Compile Include="Microsoft.AspNetCore.DataProtection.AzureStorage.netcoreapp3.0.cs" />
6+
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
7+
<Compile Include="Microsoft.AspNetCore.DataProtection.AzureStorage.netstandard2.0.cs" />
88
<Reference Include="Microsoft.AspNetCore.DataProtection" />
99
<Reference Include="Microsoft.Azure.Storage.Blob" />
1010
</ItemGroup>

src/DataProtection/AzureStorage/src/Microsoft.AspNetCore.DataProtection.AzureStorage.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<Description>Microsoft Azure Blob storage support as key store.</Description>
5-
<TargetFramework>netcoreapp3.0</TargetFramework>
5+
<TargetFramework>netstandard2.0</TargetFramework>
66
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
77
<GenerateDocumentationFile>true</GenerateDocumentationFile>
88
<PackageTags>aspnetcore;dataprotection;azure;blob</PackageTags>
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<!-- This file is automatically generated. -->
22
<Project Sdk="Microsoft.NET.Sdk">
33
<PropertyGroup>
4-
<TargetFrameworks>netcoreapp3.0</TargetFrameworks>
4+
<TargetFrameworks>netstandard2.0</TargetFrameworks>
55
</PropertyGroup>
6-
<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp3.0'">
7-
<Compile Include="Microsoft.AspNetCore.Cryptography.Internal.netcoreapp3.0.cs" />
6+
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
7+
<Compile Include="Microsoft.AspNetCore.Cryptography.Internal.netstandard2.0.cs" />
88

99
</ItemGroup>
1010
</Project>

src/DataProtection/Cryptography.Internal/src/Microsoft.AspNetCore.Cryptography.Internal.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
<PropertyGroup>
44
<Description>Infrastructure for ASP.NET Core cryptographic packages. Applications and libraries should not reference this package directly.</Description>
5-
<TargetFramework>netcoreapp3.0</TargetFramework>
5+
<TargetFramework>netstandard2.0</TargetFramework>
66
<IsAspNetCoreApp>true</IsAspNetCoreApp>
7+
<IsShippingPackage>true</IsShippingPackage>
78
<NoWarn>$(NoWarn);CS1591</NoWarn>
89
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
910
<GenerateDocumentationFile>true</GenerateDocumentationFile>
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
<!-- This file is automatically generated. -->
22
<Project Sdk="Microsoft.NET.Sdk">
33
<PropertyGroup>
4-
<TargetFrameworks>netcoreapp3.0</TargetFrameworks>
4+
<TargetFrameworks>netstandard2.0;netcoreapp2.0</TargetFrameworks>
55
</PropertyGroup>
6-
<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp3.0'">
7-
<Compile Include="Microsoft.AspNetCore.Cryptography.KeyDerivation.netcoreapp3.0.cs" />
6+
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
7+
<Compile Include="Microsoft.AspNetCore.Cryptography.KeyDerivation.netstandard2.0.cs" />
8+
<Reference Include="Microsoft.AspNetCore.Cryptography.Internal" />
9+
</ItemGroup>
10+
<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp2.0'">
11+
<Compile Include="Microsoft.AspNetCore.Cryptography.KeyDerivation.netcoreapp2.0.cs" />
812
<Reference Include="Microsoft.AspNetCore.Cryptography.Internal" />
913
</ItemGroup>
1014
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
namespace Microsoft.AspNetCore.Cryptography.KeyDerivation
5+
{
6+
public static partial class KeyDerivation
7+
{
8+
public static byte[] Pbkdf2(string password, byte[] salt, Microsoft.AspNetCore.Cryptography.KeyDerivation.KeyDerivationPrf prf, int iterationCount, int numBytesRequested) { throw null; }
9+
}
10+
public enum KeyDerivationPrf
11+
{
12+
HMACSHA1 = 0,
13+
HMACSHA256 = 1,
14+
HMACSHA512 = 2,
15+
}
16+
}

src/DataProtection/Cryptography.KeyDerivation/src/Microsoft.AspNetCore.Cryptography.KeyDerivation.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
<PropertyGroup>
44
<Description>ASP.NET Core utilities for key derivation.</Description>
5-
<TargetFramework>netcoreapp3.0</TargetFramework>
5+
<TargetFrameworks>netstandard2.0;netcoreapp2.0</TargetFrameworks>
66
<IsAspNetCoreApp>true</IsAspNetCoreApp>
7+
<IsShippingPackage>true</IsShippingPackage>
78
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
89
<GenerateDocumentationFile>true</GenerateDocumentationFile>
910
<PackageTags>aspnetcore;dataprotection</PackageTags>

src/DataProtection/Cryptography.KeyDerivation/src/PBKDF2/NetCorePbkdf2Provider.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
#if NETCOREAPP2_0
45
using System;
56
using System.Diagnostics;
67
using System.Security.Cryptography;
@@ -61,3 +62,4 @@ private static byte[] DeriveKeyImpl(string password, byte[] salt, KeyDerivationP
6162
}
6263
}
6364
}
65+
#endif

src/DataProtection/Cryptography.KeyDerivation/src/PBKDF2/Pbkdf2Util.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,15 @@ private static IPbkdf2Provider GetPbkdf2Provider()
2727
}
2828
else
2929
{
30+
#if NETSTANDARD2_0
31+
return new ManagedPbkdf2Provider();
32+
#elif NETCOREAPP2_0
3033
// fastest implementation on .NET Core for Linux/macOS.
3134
// Not supported on .NET Framework
3235
return new NetCorePbkdf2Provider();
36+
#else
37+
#error Update target frameworks
38+
#endif
3339
}
3440
}
3541
}

src/DataProtection/DataProtection/ref/Microsoft.AspNetCore.DataProtection.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
<!-- This file is automatically generated. -->
22
<Project Sdk="Microsoft.NET.Sdk">
33
<PropertyGroup>
4-
<TargetFrameworks>netcoreapp3.0</TargetFrameworks>
4+
<TargetFrameworks>netstandard2.0</TargetFrameworks>
55
</PropertyGroup>
6-
<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp3.0'">
7-
<Compile Include="Microsoft.AspNetCore.DataProtection.netcoreapp3.0.cs" />
6+
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
7+
<Compile Include="Microsoft.AspNetCore.DataProtection.netstandard2.0.cs" />
88
<Reference Include="Microsoft.AspNetCore.Cryptography.Internal" />
99
<Reference Include="Microsoft.AspNetCore.DataProtection.Abstractions" />
10-
<Reference Include="Microsoft.AspNetCore.Hosting.Abstractions" />
1110
<Reference Include="Microsoft.Extensions.DependencyInjection.Abstractions" />
11+
<Reference Include="Microsoft.Extensions.Hosting.Abstractions" />
1212
<Reference Include="Microsoft.Extensions.Logging.Abstractions" />
1313
<Reference Include="Microsoft.Extensions.Options" />
1414
<Reference Include="Microsoft.Win32.Registry" />

src/DataProtection/DataProtection/src/DataProtectionServiceCollectionExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
using Microsoft.AspNetCore.DataProtection.KeyManagement;
1010
using Microsoft.AspNetCore.DataProtection.KeyManagement.Internal;
1111
using Microsoft.AspNetCore.DataProtection.XmlEncryption;
12-
using Microsoft.AspNetCore.Hosting;
1312
using Microsoft.Extensions.DependencyInjection.Extensions;
13+
using Microsoft.Extensions.Hosting;
1414
using Microsoft.Extensions.Logging;
1515
using Microsoft.Extensions.Logging.Abstractions;
1616
using Microsoft.Extensions.Options;
@@ -77,7 +77,7 @@ private static void AddDataProtectionServices(IServiceCollection services)
7777

7878
services.TryAddSingleton<IKeyManager, XmlKeyManager>();
7979
services.TryAddSingleton<IApplicationDiscriminator, HostingApplicationDiscriminator>();
80-
services.TryAddEnumerable(ServiceDescriptor.Singleton<IStartupFilter, DataProtectionStartupFilter>());
80+
services.TryAddEnumerable(ServiceDescriptor.Singleton<IHostedService, DataProtectionHostedService>());
8181

8282
// Internal services
8383
services.TryAddSingleton<IDefaultKeyResolver, DefaultKeyResolver>();
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,32 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5-
using Microsoft.AspNetCore.Builder;
5+
using System.Threading;
6+
using System.Threading.Tasks;
67
using Microsoft.AspNetCore.DataProtection.KeyManagement.Internal;
7-
using Microsoft.AspNetCore.Hosting;
8+
using Microsoft.Extensions.Hosting;
89
using Microsoft.Extensions.Logging;
910
using Microsoft.Extensions.Logging.Abstractions;
1011

1112
namespace Microsoft.AspNetCore.DataProtection.Internal
1213
{
13-
internal class DataProtectionStartupFilter : IStartupFilter
14+
internal class DataProtectionHostedService : IHostedService
1415
{
1516
private readonly IKeyRingProvider _keyRingProvider;
16-
private readonly ILogger<DataProtectionStartupFilter> _logger;
17+
private readonly ILogger<DataProtectionHostedService> _logger;
1718

18-
public DataProtectionStartupFilter(IKeyRingProvider keyRingProvider)
19+
public DataProtectionHostedService(IKeyRingProvider keyRingProvider)
1920
: this(keyRingProvider, NullLoggerFactory.Instance)
2021
{ }
2122

22-
public DataProtectionStartupFilter(IKeyRingProvider keyRingProvider, ILoggerFactory loggerFactory)
23+
public DataProtectionHostedService(IKeyRingProvider keyRingProvider, ILoggerFactory loggerFactory)
2324
{
2425
_keyRingProvider = keyRingProvider;
25-
_logger = loggerFactory.CreateLogger<DataProtectionStartupFilter>();
26+
_logger = loggerFactory.CreateLogger<DataProtectionHostedService>();
2627
}
2728

28-
public Action<IApplicationBuilder> Configure(Action<IApplicationBuilder> next)
29+
public Task StartAsync(CancellationToken token)
2930
{
3031
try
3132
{
@@ -42,7 +43,9 @@ public Action<IApplicationBuilder> Configure(Action<IApplicationBuilder> next)
4243
_logger.KeyRingFailedToLoadOnStartup(ex);
4344
}
4445

45-
return next;
46+
return Task.CompletedTask;
4647
}
48+
49+
public Task StopAsync(CancellationToken token) => Task.CompletedTask;
4750
}
4851
}

src/DataProtection/DataProtection/src/Microsoft.AspNetCore.DataProtection.csproj

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
<PropertyGroup>
44
<Description>ASP.NET Core logic to protect and unprotect data, similar to DPAPI.</Description>
5-
<TargetFramework>netcoreapp3.0</TargetFramework>
5+
<TargetFramework>netstandard2.0</TargetFramework>
66
<IsAspNetCoreApp>true</IsAspNetCoreApp>
7+
<IsShippingPackage>true</IsShippingPackage>
78
<NoWarn>$(NoWarn);CS1591</NoWarn>
89
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
910
<GenerateDocumentationFile>true</GenerateDocumentationFile>
@@ -17,13 +18,14 @@
1718
<ItemGroup>
1819
<Reference Include="Microsoft.AspNetCore.Cryptography.Internal" />
1920
<Reference Include="Microsoft.AspNetCore.DataProtection.Abstractions" />
20-
<Reference Include="Microsoft.AspNetCore.Hosting.Abstractions" />
2121
<Reference Include="Microsoft.Extensions.DependencyInjection.Abstractions" />
22+
<Reference Include="Microsoft.Extensions.Hosting.Abstractions" />
2223
<Reference Include="Microsoft.Extensions.Logging.Abstractions" />
2324
<Reference Include="Microsoft.Extensions.Options" />
2425
<Reference Include="Microsoft.Win32.Registry" />
2526
<Reference Include="System.Security.Cryptography.Xml" />
2627
<Reference Include="System.Security.Principal.Windows" />
28+
<SuppressBaselineReference Include="Microsoft.AspNetCore.Hosting.Abstractions" />
2729
</ItemGroup>
2830

2931
</Project>

src/DataProtection/DataProtection/test/HostingTests.cs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
@@ -7,6 +7,7 @@
77
using Microsoft.AspNetCore.Builder;
88
using Microsoft.AspNetCore.DataProtection.KeyManagement.Internal;
99
using Microsoft.AspNetCore.Hosting;
10+
using Microsoft.Extensions.Hosting;
1011
using Microsoft.AspNetCore.Hosting.Server;
1112
using Microsoft.AspNetCore.Http.Features;
1213
using Microsoft.AspNetCore.Testing;
@@ -20,7 +21,7 @@ namespace Microsoft.AspNetCore.DataProtection.Test
2021
public class HostingTests
2122
{
2223
[Fact]
23-
public async Task LoadsKeyRingBeforeServerStarts()
24+
public async Task WebhostLoadsKeyRingBeforeServerStarts()
2425
{
2526
var tcs = new TaskCompletionSource<object>();
2627
var mockKeyRing = new Mock<IKeyRingProvider>();
@@ -46,6 +47,33 @@ public async Task LoadsKeyRingBeforeServerStarts()
4647
mockKeyRing.VerifyAll();
4748
}
4849

50+
[Fact]
51+
public async Task GenericHostLoadsKeyRingBeforeServerStarts()
52+
{
53+
var tcs = new TaskCompletionSource<object>();
54+
var mockKeyRing = new Mock<IKeyRingProvider>();
55+
mockKeyRing.Setup(m => m.GetCurrentKeyRing())
56+
.Returns(Mock.Of<IKeyRing>())
57+
.Callback(() => tcs.TrySetResult(null));
58+
59+
var builder = new HostBuilder()
60+
.ConfigureServices(s =>
61+
s.AddDataProtection()
62+
.Services
63+
.Replace(ServiceDescriptor.Singleton(mockKeyRing.Object))
64+
.AddSingleton<IServer>(
65+
new FakeServer(onStart: () => tcs.TrySetException(new InvalidOperationException("Server was started before key ring was initialized")))))
66+
.ConfigureWebHost(b => b.UseStartup<TestStartup>());
67+
68+
using (var host = builder.Build())
69+
{
70+
await host.StartAsync();
71+
}
72+
73+
await tcs.Task.TimeoutAfter(TimeSpan.FromSeconds(10));
74+
mockKeyRing.VerifyAll();
75+
}
76+
4977
[Fact]
5078
public async Task StartupContinuesOnFailureToLoadKey()
5179
{

0 commit comments

Comments
 (0)