4
4
using System ;
5
5
using System . Collections . Generic ;
6
6
using System . Diagnostics ;
7
+ using System . IO ;
7
8
using System . Linq ;
8
9
using System . Net ;
9
10
using System . Net . Http ;
@@ -30,6 +31,9 @@ public class AspNetProcess : IDisposable
30
31
private readonly HttpClient _httpClient ;
31
32
private readonly ITestOutputHelper _output ;
32
33
34
+ private string _certificatePath ;
35
+ private string _certificatePassword = Guid . NewGuid ( ) . ToString ( ) ;
36
+
33
37
internal readonly Uri ListeningUri ;
34
38
internal ProcessEx Process { get ; }
35
39
@@ -48,12 +52,14 @@ public AspNetProcess(
48
52
AllowAutoRedirect = true ,
49
53
UseCookies = true ,
50
54
CookieContainer = new CookieContainer ( ) ,
51
- ServerCertificateCustomValidationCallback = ( m , c , ch , p ) => true ,
55
+ ServerCertificateCustomValidationCallback = HttpClientHandler . DangerousAcceptAnyServerCertificateValidator ,
52
56
} )
53
57
{
54
58
Timeout = TimeSpan . FromMinutes ( 2 )
55
59
} ;
56
60
61
+ _certificatePath = Path . Combine ( workingDirectory , $ "{ Guid . NewGuid ( ) } .pfx") ;
62
+
57
63
EnsureDevelopmentCertificates ( ) ;
58
64
59
65
output . WriteLine ( "Running ASP.NET application..." ) ;
@@ -62,7 +68,13 @@ public AspNetProcess(
62
68
63
69
logger ? . LogInformation ( $ "AspNetProcess - process: { DotNetMuxer . MuxerPathOrDefault ( ) } arguments: { arguments } ") ;
64
70
65
- Process = ProcessEx . Run ( output , workingDirectory , DotNetMuxer . MuxerPathOrDefault ( ) , arguments , envVars : environmentVariables ) ;
71
+ var finalEnvironmentVariables = new Dictionary < string , string > ( environmentVariables )
72
+ {
73
+ [ "ASPNETCORE_KESTREL__CERTIFICATES__DEFAULT__PATH" ] = _certificatePath ,
74
+ [ "ASPNETCORE_KESTREL__CERTIFICATES__DEFAULT__PASSWORD" ] = _certificatePassword
75
+ } ;
76
+
77
+ Process = ProcessEx . Run ( output , workingDirectory , DotNetMuxer . MuxerPathOrDefault ( ) , arguments , envVars : finalEnvironmentVariables ) ;
66
78
67
79
logger ? . LogInformation ( "AspNetProcess - process started" ) ;
68
80
@@ -74,10 +86,12 @@ public AspNetProcess(
74
86
}
75
87
}
76
88
77
- internal static void EnsureDevelopmentCertificates ( )
89
+ internal void EnsureDevelopmentCertificates ( )
78
90
{
79
91
var now = DateTimeOffset . Now ;
80
- new CertificateManager ( ) . EnsureAspNetCoreHttpsDevelopmentCertificate ( now , now . AddYears ( 1 ) ) ;
92
+ var manager = new CertificateManager ( ) ;
93
+ var certificate = manager . CreateAspNetCoreHttpsDevelopmentCertificate ( now , now . AddYears ( 1 ) , "CN=localhost" ) ;
94
+ manager . ExportCertificate ( certificate , path : _certificatePath , includePrivateKey : true , _certificatePassword ) ;
81
95
}
82
96
83
97
public void VisitInBrowser ( IWebDriver driver )
0 commit comments