2
2
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3
3
4
4
using System ;
5
- using System . Collections . Generic ;
6
5
using System . Linq ;
7
6
using System . Security . Cryptography . X509Certificates ;
7
+ using Microsoft . AspNetCore . Certificates . Generation ;
8
8
using Microsoft . AspNetCore . Hosting ;
9
9
using Microsoft . AspNetCore . Server . Kestrel . Core ;
10
10
using Microsoft . AspNetCore . Server . Kestrel . Core . Internal ;
@@ -13,7 +13,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel
13
13
{
14
14
internal class DefaultHttpsProvider : IDefaultHttpsProvider
15
15
{
16
- private const string AspNetHttpsOid = "1.3.6.1.4.1.311.84.1.1" ;
16
+ private static readonly CertificateManager _certificateManager = new CertificateManager ( ) ;
17
17
18
18
public void ConfigureHttps ( ListenOptions listenOptions )
19
19
{
@@ -22,54 +22,16 @@ public void ConfigureHttps(ListenOptions listenOptions)
22
22
23
23
private static X509Certificate2 FindDevelopmentCertificate ( )
24
24
{
25
- // TODO: replace this with call to
26
- // CertificateManager.FindCertificates(CertificatePurpose.HTTPS, StoreName.My, StoreLocation.CurrentUser, isValid: true)
27
- // when that becomes available.
28
- using ( var store = new X509Store ( StoreName . My , StoreLocation . CurrentUser ) )
29
- {
30
- store . Open ( OpenFlags . ReadOnly ) ;
31
-
32
- var certificates = store . Certificates . OfType < X509Certificate2 > ( ) ;
33
- var certificate = certificates
34
- . FirstOrDefault ( c => HasOid ( c , AspNetHttpsOid ) && ! IsExpired ( c ) && HasPrivateKey ( c ) ) ;
35
-
36
- if ( certificate == null )
37
- {
38
- throw new InvalidOperationException ( "Unable to find ASP.NET Core development certificate." ) ;
39
- }
40
-
41
- DisposeCertificates ( certificates . Except ( new [ ] { certificate } ) ) ;
42
-
43
- return certificate ;
44
- }
45
- }
25
+ var certificate = _certificateManager
26
+ . ListCertificates ( CertificatePurpose . HTTPS , StoreName . My , StoreLocation . CurrentUser , isValid : true , requireExportable : false )
27
+ . FirstOrDefault ( ) ;
46
28
47
- private static bool HasOid ( X509Certificate2 certificate , string oid ) =>
48
- certificate . Extensions
49
- . OfType < X509Extension > ( )
50
- . Any ( e => string . Equals ( oid , e . Oid . Value , StringComparison . Ordinal ) ) ;
51
-
52
- private static bool IsExpired ( X509Certificate2 certificate )
53
- {
54
- var now = DateTimeOffset . Now ;
55
- return now < certificate . NotBefore || now > certificate . NotAfter ;
56
- }
57
-
58
- private static bool HasPrivateKey ( X509Certificate2 certificate )
59
- => certificate . GetRSAPrivateKey ( ) != null ;
60
-
61
- private static void DisposeCertificates ( IEnumerable < X509Certificate2 > certificates )
62
- {
63
- foreach ( var certificate in certificates )
29
+ if ( certificate == null )
64
30
{
65
- try
66
- {
67
- certificate . Dispose ( ) ;
68
- }
69
- catch
70
- {
71
- }
31
+ throw new InvalidOperationException ( "Unable to find ASP.NET Core development certificate." ) ;
72
32
}
33
+
34
+ return certificate ;
73
35
}
74
36
}
75
37
}
0 commit comments