Skip to content

[Templates] Switch to use a PFX certificate instead of one from the store #20494

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 3, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions src/ProjectTemplates/test/Helpers/AspNetProcess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
Expand All @@ -30,6 +31,9 @@ public class AspNetProcess : IDisposable
private readonly HttpClient _httpClient;
private readonly ITestOutputHelper _output;

private string _certificatePath;
private string _certificatePassword = Guid.NewGuid().ToString();

internal readonly Uri ListeningUri;
internal ProcessEx Process { get; }

Expand All @@ -48,12 +52,14 @@ public AspNetProcess(
AllowAutoRedirect = true,
UseCookies = true,
CookieContainer = new CookieContainer(),
ServerCertificateCustomValidationCallback = (m, c, ch, p) => true,
ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator,
})
{
Timeout = TimeSpan.FromMinutes(2)
};

_certificatePath = Path.Combine(workingDirectory, $"{Guid.NewGuid()}.pfx");

EnsureDevelopmentCertificates();

output.WriteLine("Running ASP.NET application...");
Expand All @@ -62,7 +68,13 @@ public AspNetProcess(

logger?.LogInformation($"AspNetProcess - process: {DotNetMuxer.MuxerPathOrDefault()} arguments: {arguments}");

Process = ProcessEx.Run(output, workingDirectory, DotNetMuxer.MuxerPathOrDefault(), arguments, envVars: environmentVariables);
var finalEnvironmentVariables = new Dictionary<string, string>(environmentVariables)
{
["ASPNETCORE_KESTREL__CERTIFICATES__DEFAULT__PATH"] = _certificatePath,
["ASPNETCORE_KESTREL__CERTIFICATES__DEFAULT__PASSWORD"] = _certificatePassword
};

Process = ProcessEx.Run(output, workingDirectory, DotNetMuxer.MuxerPathOrDefault(), arguments, envVars: finalEnvironmentVariables);

logger?.LogInformation("AspNetProcess - process started");

Expand All @@ -74,10 +86,12 @@ public AspNetProcess(
}
}

internal static void EnsureDevelopmentCertificates()
internal void EnsureDevelopmentCertificates()
{
var now = DateTimeOffset.Now;
new CertificateManager().EnsureAspNetCoreHttpsDevelopmentCertificate(now, now.AddYears(1));
var manager = new CertificateManager();
var certificate = manager.CreateAspNetCoreHttpsDevelopmentCertificate(now, now.AddYears(1), "CN=localhost");
manager.ExportCertificate(certificate, path: _certificatePath, includePrivateKey: true, _certificatePassword);
}

public void VisitInBrowser(IWebDriver driver)
Expand Down