Skip to content

[WebApplicationFactory] UseKestrel with SSL feedback #63012

@fschwiet

Description

@fschwiet

Is there an existing issue for this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe the problem.

First of all, thank you for adding UseKestrel I am happy to be able to write browser-based integration tests now.

Getting SSL working was a bit tricky so I wanted to give feedback. For context I am using SSL certificates provided by dotnet dev-certs, and Selenium ChromeDriver as well as the HttpClient returned by CreateClient. I was only interested in using SSL, but might need both SSL and non-SSL connections in the future.

Adding the following to the override of WebApplicationFactory worked to get SSL mostly working:

	protected override void ConfigureWebHost(IWebHostBuilder builder)
	{
		builder.ConfigureKestrel(options =>
		{
			options.Listen(
				IPAddress.Loopback,
				ConfiguredPort,
				listenOptions => listenOptions.UseHttps()
			);
		});

		...

I use a predetermined "ConfiguredPort" because I need to set a configure value with the applications root url before starting the server.

The problem is HttpClient returned by CreateClient doesn't work:

  • it is using the http schema, not https.
  • it is using an IP address, and my dev-certs are for localhost

I worked around this with:

        HttpClient CreateClient()
	{
		var result = serverFactory.CreateClient();

		var uriBuilder = new UriBuilder(result.BaseAddress!);

		uriBuilder.Host = "localhost";
		uriBuilder.Scheme = "https";

		result.BaseAddress = uriBuilder.Uri;

		return result;
	}

It works, but could be easier or at least have some documentation on how to proceed with SSL.

Last, its minor, but it would be nice to document when to call UseKestrel(). I initially tried it from my derived classes constructor, that didn't work, calling it from an override of InitializeAsync did work.

Describe the solution you'd like

Not sure

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-mvcIncludes: MVC, Actions and Controllers, Localization, CORS, most templatesfeature-mvc-testingMVC testing package

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions