@@ -115,52 +115,55 @@ public void System_UsesProvidedDirectory_WithConfigurationCallback()
115115
116116 [ ConditionalFact ]
117117 [ X509StoreIsAvailable ( StoreName . My , StoreLocation . CurrentUser ) ]
118- [ SkipOnHelix ( "https://github.com/dotnet/aspnetcore/issues/6720 and https://github.com/dotnet/aspnetcore/issues/26871 " , Queues = "All.OSX;Windows.10.Arm64;Windows.10.Arm64.Open;Windows.10.Arm64v8;Windows.10.Arm64v8.Open " ) ]
118+ [ SkipOnHelix ( "https://github.com/dotnet/aspnetcore/issues/6720" , Queues = "All.OSX" ) ]
119119 public void System_UsesProvidedDirectoryAndCertificate ( )
120120 {
121121 var filePath = Path . Combine ( GetTestFilesPath ( ) , "TestCert.pfx" ) ;
122- using ( var store = new X509Store ( StoreName . My , StoreLocation . CurrentUser ) )
123- {
124- store . Open ( OpenFlags . ReadWrite ) ;
125- store . Add ( new X509Certificate2 ( filePath , "password" , X509KeyStorageFlags . Exportable ) ) ;
126- store . Close ( ) ;
127- }
128-
129- WithUniqueTempDirectory ( directory =>
122+ using ( var imported = new X509Certificate2 ( filePath , "password" , X509KeyStorageFlags . Exportable ) )
130123 {
131- var certificateStore = new X509Store ( StoreName . My , StoreLocation . CurrentUser ) ;
132- certificateStore . Open ( OpenFlags . ReadWrite ) ;
133- var certificate = certificateStore . Certificates . Find ( X509FindType . FindBySubjectName , "TestCert" , false ) [ 0 ] ;
134- Assert . True ( certificate . HasPrivateKey , "Cert should have a private key" ) ;
135- try
124+ using ( var store = new X509Store ( StoreName . My , StoreLocation . CurrentUser ) )
136125 {
137- // Step 1: directory should be completely empty
138- directory . Create ( ) ;
139- Assert . Empty ( directory . GetFiles ( ) ) ;
140-
141- // Step 2: instantiate the system and round-trip a payload
142- var protector = DataProtectionProvider . Create ( directory , certificate ) . CreateProtector ( "purpose" ) ;
143- var data = protector . Protect ( "payload" ) ;
144-
145- // add a cert without the private key to ensure the decryption will still fallback to the cert store
146- var certWithoutKey = new X509Certificate2 ( Path . Combine ( GetTestFilesPath ( ) , "TestCertWithoutPrivateKey.pfx" ) , "password" ) ;
147- var unprotector = DataProtectionProvider . Create ( directory , o => o . UnprotectKeysWithAnyCertificate ( certWithoutKey ) ) . CreateProtector ( "purpose" ) ;
148- Assert . Equal ( "payload" , unprotector . Unprotect ( data ) ) ;
149-
150- // Step 3: validate that there's now a single key in the directory and that it's is protected using the certificate
151- var allFiles = directory . GetFiles ( ) ;
152- Assert . Single ( allFiles ) ;
153- Assert . StartsWith ( "key-" , allFiles [ 0 ] . Name , StringComparison . OrdinalIgnoreCase ) ;
154- string fileText = File . ReadAllText ( allFiles [ 0 ] . FullName ) ;
155- Assert . DoesNotContain ( "Warning: the key below is in an unencrypted form." , fileText , StringComparison . Ordinal ) ;
156- Assert . Contains ( "X509Certificate" , fileText , StringComparison . Ordinal ) ;
126+ store . Open ( OpenFlags . ReadWrite ) ;
127+ store . Add ( imported ) ;
128+ store . Close ( ) ;
157129 }
158- finally
130+
131+ WithUniqueTempDirectory ( directory =>
159132 {
160- certificateStore . Remove ( certificate ) ;
161- certificateStore . Close ( ) ;
162- }
163- } ) ;
133+ var certificateStore = new X509Store ( StoreName . My , StoreLocation . CurrentUser ) ;
134+ certificateStore . Open ( OpenFlags . ReadWrite ) ;
135+ var certificate = certificateStore . Certificates . Find ( X509FindType . FindBySubjectName , "TestCert" , false ) [ 0 ] ;
136+ Assert . True ( certificate . HasPrivateKey , "Cert should have a private key" ) ;
137+ try
138+ {
139+ // Step 1: directory should be completely empty
140+ directory . Create ( ) ;
141+ Assert . Empty ( directory . GetFiles ( ) ) ;
142+
143+ // Step 2: instantiate the system and round-trip a payload
144+ var protector = DataProtectionProvider . Create ( directory , certificate ) . CreateProtector ( "purpose" ) ;
145+ var data = protector . Protect ( "payload" ) ;
146+
147+ // add a cert without the private key to ensure the decryption will still fallback to the cert store
148+ var certWithoutKey = new X509Certificate2 ( Path . Combine ( GetTestFilesPath ( ) , "TestCertWithoutPrivateKey.pfx" ) , "password" ) ;
149+ var unprotector = DataProtectionProvider . Create ( directory , o => o . UnprotectKeysWithAnyCertificate ( certWithoutKey ) ) . CreateProtector ( "purpose" ) ;
150+ Assert . Equal ( "payload" , unprotector . Unprotect ( data ) ) ;
151+
152+ // Step 3: validate that there's now a single key in the directory and that it's is protected using the certificate
153+ var allFiles = directory . GetFiles ( ) ;
154+ Assert . Single ( allFiles ) ;
155+ Assert . StartsWith ( "key-" , allFiles [ 0 ] . Name , StringComparison . OrdinalIgnoreCase ) ;
156+ string fileText = File . ReadAllText ( allFiles [ 0 ] . FullName ) ;
157+ Assert . DoesNotContain ( "Warning: the key below is in an unencrypted form." , fileText , StringComparison . Ordinal ) ;
158+ Assert . Contains ( "X509Certificate" , fileText , StringComparison . Ordinal ) ;
159+ }
160+ finally
161+ {
162+ certificateStore . Remove ( certificate ) ;
163+ certificateStore . Close ( ) ;
164+ }
165+ } ) ;
166+ }
164167 }
165168
166169 [ ConditionalFact ]
0 commit comments