1
1
// ReSharper disable StringLiteralTypo
2
+ // ReSharper disable LoopCanBeConvertedToQuery
2
3
namespace Atc . Installer . Integration . InternetInformationServer ;
3
4
4
5
/// <summary>
@@ -168,7 +169,7 @@ public bool IsComponentInstalledUrlRewriteModule2()
168
169
}
169
170
170
171
public string ? ResolvedVirtualRootFolder (
171
- string folder )
172
+ string ? folder )
172
173
=> folder is not null &&
173
174
folder . StartsWith ( @".\" , StringComparison . Ordinal ) &&
174
175
IsInstalled &&
@@ -184,7 +185,7 @@ public bool IsComponentInstalledUrlRewriteModule2()
184
185
arguments : "unlock config -section:\" system.webServer/modules\" " )
185
186
. ConfigureAwait ( false ) ;
186
187
187
- if ( isSuccessful && output is not null )
188
+ if ( isSuccessful && ! string . IsNullOrEmpty ( output ) )
188
189
{
189
190
return ( IsSucceeded : false , ErrorMessage : null ) ;
190
191
}
@@ -222,7 +223,7 @@ await FileHelper
222
223
public ComponentRunningState GetApplicationPoolState (
223
224
string applicationPoolName )
224
225
{
225
- ArgumentNullException . ThrowIfNull ( applicationPoolName ) ;
226
+ ArgumentException . ThrowIfNullOrEmpty ( applicationPoolName ) ;
226
227
227
228
try
228
229
{
@@ -252,7 +253,7 @@ public ComponentRunningState GetApplicationPoolState(
252
253
public ComponentRunningState GetWebsiteState (
253
254
string websiteName )
254
255
{
255
- ArgumentNullException . ThrowIfNull ( websiteName ) ;
256
+ ArgumentException . ThrowIfNullOrEmpty ( websiteName ) ;
256
257
257
258
try
258
259
{
@@ -285,7 +286,7 @@ public Task<bool> CreateApplicationPool(
285
286
ushort timeoutInSeconds = 60 ,
286
287
CancellationToken cancellationToken = default )
287
288
{
288
- ArgumentNullException . ThrowIfNull ( applicationPoolName ) ;
289
+ ArgumentException . ThrowIfNullOrEmpty ( applicationPoolName ) ;
289
290
290
291
var applicationPoolState = GetApplicationPoolState ( applicationPoolName ) ;
291
292
if ( applicationPoolState != ComponentRunningState . NotAvailable )
@@ -311,6 +312,38 @@ public Task<bool> CreateApplicationPool(
311
312
}
312
313
}
313
314
315
+ public Task < bool > DeleteApplicationPool (
316
+ string applicationPoolName ,
317
+ ushort timeoutInSeconds = 60 ,
318
+ CancellationToken cancellationToken = default )
319
+ {
320
+ ArgumentException . ThrowIfNullOrEmpty ( applicationPoolName ) ;
321
+
322
+ var applicationPoolState = GetApplicationPoolState ( applicationPoolName ) ;
323
+ if ( applicationPoolState == ComponentRunningState . NotAvailable )
324
+ {
325
+ return Task . FromResult ( false ) ;
326
+ }
327
+
328
+ try
329
+ {
330
+ using var serverManager = new ServerManager ( ) ;
331
+ var applicationPool = serverManager . ApplicationPools [ applicationPoolName ] ;
332
+ if ( applicationPool is null )
333
+ {
334
+ return Task . FromResult ( false ) ;
335
+ }
336
+
337
+ applicationPool . Delete ( ) ;
338
+ serverManager . CommitChanges ( ) ;
339
+ return Task . FromResult ( true ) ;
340
+ }
341
+ catch
342
+ {
343
+ return Task . FromResult ( false ) ;
344
+ }
345
+ }
346
+
314
347
public Task < bool > CreateWebsite (
315
348
string websiteName ,
316
349
string applicationPoolName ,
@@ -345,8 +378,8 @@ public async Task<bool> CreateWebsite(
345
378
ushort timeoutInSeconds = 60 ,
346
379
CancellationToken cancellationToken = default )
347
380
{
348
- ArgumentNullException . ThrowIfNull ( websiteName ) ;
349
- ArgumentNullException . ThrowIfNull ( applicationPoolName ) ;
381
+ ArgumentException . ThrowIfNullOrEmpty ( websiteName ) ;
382
+ ArgumentException . ThrowIfNullOrEmpty ( applicationPoolName ) ;
350
383
ArgumentNullException . ThrowIfNull ( physicalPath ) ;
351
384
352
385
var websiteState = GetWebsiteState ( websiteName ) ;
@@ -416,6 +449,38 @@ public async Task<bool> CreateWebsite(
416
449
}
417
450
}
418
451
452
+ public Task < bool > DeleteWebsite (
453
+ string websiteName ,
454
+ ushort timeoutInSeconds = 60 ,
455
+ CancellationToken cancellationToken = default )
456
+ {
457
+ ArgumentException . ThrowIfNullOrEmpty ( websiteName ) ;
458
+
459
+ var websiteState = GetWebsiteState ( websiteName ) ;
460
+ if ( websiteState == ComponentRunningState . NotAvailable )
461
+ {
462
+ return Task . FromResult ( false ) ;
463
+ }
464
+
465
+ try
466
+ {
467
+ using var serverManager = new ServerManager ( ) ;
468
+ var site = serverManager . Sites [ websiteName ] ;
469
+ if ( site is null )
470
+ {
471
+ return Task . FromResult ( false ) ;
472
+ }
473
+
474
+ site . Delete ( ) ;
475
+ serverManager . CommitChanges ( ) ;
476
+ return Task . FromResult ( true ) ;
477
+ }
478
+ catch
479
+ {
480
+ return Task . FromResult ( false ) ;
481
+ }
482
+ }
483
+
419
484
public async Task < bool > StopApplicationPool (
420
485
string applicationPoolName ,
421
486
ushort timeoutInSeconds = 60 ,
@@ -604,6 +669,118 @@ public async Task<bool> StartWebsiteAndApplicationPool(
604
669
=> await StartApplicationPool ( applicationPoolName , timeoutInSeconds , cancellationToken ) . ConfigureAwait ( false ) &&
605
670
await StartWebsite ( websiteName , timeoutInSeconds , cancellationToken ) . ConfigureAwait ( false ) ;
606
671
672
+ public IList < X509Certificate2 > GetX509Certificates ( )
673
+ => CryptographyHelper . GetX509Certificates ( ) ;
674
+
675
+ public X509Certificate2 ? GetWebsiteX509Certificate (
676
+ string websiteName )
677
+ {
678
+ ArgumentException . ThrowIfNullOrEmpty ( websiteName ) ;
679
+
680
+ try
681
+ {
682
+ using var serverManager = new ServerManager ( ) ;
683
+ var site = serverManager . Sites [ websiteName ] ;
684
+ if ( site is null )
685
+ {
686
+ return null ;
687
+ }
688
+
689
+ foreach ( var siteBinding in site . Bindings )
690
+ {
691
+ if ( siteBinding . SslFlags == SslFlags . None ||
692
+ ! "https" . Equals ( siteBinding . Protocol , StringComparison . OrdinalIgnoreCase ) )
693
+ {
694
+ continue ;
695
+ }
696
+
697
+ return CryptographyHelper . FindX509Certificate (
698
+ siteBinding . CertificateHash . ToHex ( ) ,
699
+ Enum < StoreName > . Parse ( siteBinding . CertificateStoreName ) ) ;
700
+ }
701
+
702
+ return null ;
703
+ }
704
+ catch
705
+ {
706
+ return null ;
707
+ }
708
+ }
709
+
710
+ public bool AssignX509CertificateToWebsite (
711
+ string websiteName ,
712
+ X509Certificate2 certificate )
713
+ {
714
+ ArgumentException . ThrowIfNullOrEmpty ( websiteName ) ;
715
+ ArgumentNullException . ThrowIfNull ( certificate ) ;
716
+
717
+ try
718
+ {
719
+ using var serverManager = new ServerManager ( ) ;
720
+ var site = serverManager . Sites [ websiteName ] ;
721
+ if ( site is null )
722
+ {
723
+ return false ;
724
+ }
725
+
726
+ foreach ( var siteBinding in site . Bindings )
727
+ {
728
+ if ( ! "https" . Equals ( siteBinding . Protocol , StringComparison . OrdinalIgnoreCase ) )
729
+ {
730
+ continue ;
731
+ }
732
+
733
+ siteBinding . CertificateHash = certificate . GetCertHash ( ) ;
734
+ siteBinding . CertificateStoreName = nameof ( StoreName . My ) ;
735
+ siteBinding . SslFlags = SslFlags . Sni ;
736
+ serverManager . CommitChanges ( ) ;
737
+ return true ;
738
+ }
739
+
740
+ return false ;
741
+ }
742
+ catch
743
+ {
744
+ return false ;
745
+ }
746
+ }
747
+
748
+ public bool UnAssignX509CertificateOnWebsite (
749
+ string websiteName )
750
+ {
751
+ ArgumentException . ThrowIfNullOrEmpty ( websiteName ) ;
752
+
753
+ try
754
+ {
755
+ using var serverManager = new ServerManager ( ) ;
756
+ var site = serverManager . Sites [ websiteName ] ;
757
+ if ( site is null )
758
+ {
759
+ return false ;
760
+ }
761
+
762
+ foreach ( var siteBinding in site . Bindings )
763
+ {
764
+ if ( ! "https" . Equals ( siteBinding . Protocol , StringComparison . OrdinalIgnoreCase ) )
765
+ {
766
+ continue ;
767
+ }
768
+
769
+ siteBinding . CertificateHash = null ;
770
+ siteBinding . CertificateStoreName = null ;
771
+ siteBinding . SslFlags = SslFlags . None ;
772
+ serverManager . CommitChanges ( ) ;
773
+ return true ;
774
+ }
775
+
776
+ return false ;
777
+ }
778
+ catch
779
+ {
780
+ return false ;
781
+ }
782
+ }
783
+
607
784
private string ? GetResourceDefaultNodeJsUrlWebConfig ( )
608
785
=> GetResourceTextFile ( "default-nodejs-urlrewrite-webconfig" ) ;
609
786
0 commit comments