@@ -70,48 +70,46 @@ public IList<X509Certificate2> ListCertificates(
70
70
var certificates = new List < X509Certificate2 > ( ) ;
71
71
try
72
72
{
73
- using ( var store = new X509Store ( storeName , location ) )
74
- {
75
- store . Open ( OpenFlags . ReadOnly ) ;
76
- certificates . AddRange ( store . Certificates . OfType < X509Certificate2 > ( ) ) ;
77
- IEnumerable < X509Certificate2 > matchingCertificates = certificates ;
78
- matchingCertificates = matchingCertificates
79
- . Where ( c => HasOid ( c , AspNetHttpsOid ) ) ;
73
+ using var store = new X509Store ( storeName , location ) ;
74
+ store . Open ( OpenFlags . ReadOnly ) ;
75
+ certificates . AddRange ( store . Certificates . OfType < X509Certificate2 > ( ) ) ;
76
+ IEnumerable < X509Certificate2 > matchingCertificates = certificates ;
77
+ matchingCertificates = matchingCertificates
78
+ . Where ( c => HasOid ( c , AspNetHttpsOid ) ) ;
80
79
81
- Log . DescribeFoundCertificates ( ToCertificateDescription ( matchingCertificates ) ) ;
80
+ Log . DescribeFoundCertificates ( ToCertificateDescription ( matchingCertificates ) ) ;
82
81
83
- if ( isValid )
84
- {
85
- // Ensure the certificate hasn't expired, has a private key and its exportable
86
- // (for container/unix scenarios).
87
- Log . CheckCertificatesValidity ( ) ;
88
- var now = DateTimeOffset . Now ;
89
- var validCertificates = matchingCertificates
90
- . Where ( c => c . NotBefore <= now &&
91
- now <= c . NotAfter &&
92
- ( ! requireExportable || IsExportable ( c ) )
93
- && MatchesVersion ( c ) )
94
- . ToArray ( ) ;
95
-
96
- var invalidCertificates = matchingCertificates . Except ( validCertificates ) ;
97
-
98
- Log . DescribeValidCertificates ( ToCertificateDescription ( validCertificates ) ) ;
99
- Log . DescribeInvalidValidCertificates ( ToCertificateDescription ( invalidCertificates ) ) ;
100
-
101
- matchingCertificates = validCertificates ;
102
- }
82
+ if ( isValid )
83
+ {
84
+ // Ensure the certificate hasn't expired, has a private key and its exportable
85
+ // (for container/unix scenarios).
86
+ Log . CheckCertificatesValidity ( ) ;
87
+ var now = DateTimeOffset . Now ;
88
+ var validCertificates = matchingCertificates
89
+ . Where ( c => c . NotBefore <= now &&
90
+ now <= c . NotAfter &&
91
+ ( ! requireExportable || IsExportable ( c ) )
92
+ && MatchesVersion ( c ) )
93
+ . ToArray ( ) ;
94
+
95
+ var invalidCertificates = matchingCertificates . Except ( validCertificates ) ;
96
+
97
+ Log . DescribeValidCertificates ( ToCertificateDescription ( validCertificates ) ) ;
98
+ Log . DescribeInvalidValidCertificates ( ToCertificateDescription ( invalidCertificates ) ) ;
99
+
100
+ matchingCertificates = validCertificates ;
101
+ }
103
102
104
- // We need to enumerate the certificates early to prevent disposing issues.
105
- matchingCertificates = matchingCertificates . ToList ( ) ;
103
+ // We need to enumerate the certificates early to prevent disposing issues.
104
+ matchingCertificates = matchingCertificates . ToList ( ) ;
106
105
107
- var certificatesToDispose = certificates . Except ( matchingCertificates ) ;
108
- DisposeCertificates ( certificatesToDispose ) ;
106
+ var certificatesToDispose = certificates . Except ( matchingCertificates ) ;
107
+ DisposeCertificates ( certificatesToDispose ) ;
109
108
110
- store . Close ( ) ;
109
+ store . Close ( ) ;
111
110
112
- Log . ListCertificatesEnd ( ) ;
113
- return ( IList < X509Certificate2 > ) matchingCertificates ;
114
- }
111
+ Log . ListCertificatesEnd ( ) ;
112
+ return ( IList < X509Certificate2 > ) matchingCertificates ;
115
113
}
116
114
catch ( Exception e )
117
115
{
@@ -174,6 +172,7 @@ public EnsureCertificateResult EnsureAspNetCoreHttpsDevelopmentCertificate(
174
172
if ( certificates . Any ( ) )
175
173
{
176
174
certificate = certificates . First ( ) ;
175
+ var failedToFixCertificateState = false ;
177
176
if ( isInteractive )
178
177
{
179
178
// Skip this step if the command is not interactive,
@@ -193,12 +192,16 @@ public EnsureCertificateResult EnsureAspNetCoreHttpsDevelopmentCertificate(
193
192
{
194
193
Log . CorrectCertificateStateError ( e . ToString ( ) ) ;
195
194
result = EnsureCertificateResult . FailedToMakeKeyAccessible ;
196
- return result ;
195
+ // We don't return early on this type of failure to allow for tooling to
196
+ // export or trust the certificate even in this situation, as that enables
197
+ // exporting the certificate to perform any necessary fix with native tooling.
198
+ failedToFixCertificateState = true ;
197
199
}
198
200
}
199
201
}
200
202
}
201
- else
203
+
204
+ if ( ! failedToFixCertificateState )
202
205
{
203
206
Log . ValidCertificatesFound ( ToCertificateDescription ( certificates ) ) ;
204
207
certificate = certificates . First ( ) ;
@@ -244,8 +247,10 @@ public EnsureCertificateResult EnsureAspNetCoreHttpsDevelopmentCertificate(
244
247
catch ( Exception e )
245
248
{
246
249
Log . CorrectCertificateStateError ( e . ToString ( ) ) ;
250
+ // We don't return early on this type of failure to allow for tooling to
251
+ // export or trust the certificate even in this situation, as that enables
252
+ // exporting the certificate to perform any necessary fix with native tooling.
247
253
result = EnsureCertificateResult . FailedToMakeKeyAccessible ;
248
- return result ;
249
254
}
250
255
}
251
256
}
@@ -259,7 +264,11 @@ public EnsureCertificateResult EnsureAspNetCoreHttpsDevelopmentCertificate(
259
264
catch ( Exception e )
260
265
{
261
266
Log . ExportCertificateError ( e . ToString ( ) ) ;
262
- result = EnsureCertificateResult . ErrorExportingTheCertificate ;
267
+ // We don't want to mask the original source of the error here.
268
+ result = result != EnsureCertificateResult . Succeeded || result != EnsureCertificateResult . ValidCertificatePresent ?
269
+ result :
270
+ EnsureCertificateResult . ErrorExportingTheCertificate ;
271
+
263
272
return result ;
264
273
}
265
274
}
@@ -751,12 +760,6 @@ public CheckCertificateStateResult(bool result, string message)
751
760
Result = result ;
752
761
Message = message ;
753
762
}
754
-
755
- public void Deconstruct ( out bool result , out string message )
756
- {
757
- result = Result ;
758
- message = Message ;
759
- }
760
763
}
761
764
762
765
internal enum RemoveLocations
0 commit comments