Skip to content
This repository has been archived by the owner on Nov 1, 2018. It is now read-only.

App hangs on first request if both SSL in Kestrel and IISIntegration are enabled #361

Closed
danroth27 opened this issue Apr 25, 2017 · 7 comments
Assignees

Comments

@danroth27
Copy link
Member

danroth27 commented Apr 25, 2017

If I enable both SSL in Kestrel and IISIntegration the app hangs on the first request when I run it in VS using IIS Express:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

using Microsoft.AspNetCore.Server.Kestrel;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.AspNetCore.Server.Kestrel.Https;
using System.Security.Cryptography.X509Certificates;
using System.Net;

namespace mvc_individual
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var host = new WebHostBuilder()
                .UseKestrel(options =>
                {
                    options.Listen(IPAddress.Loopback, 44316, listenOptions =>
                    {
                        listenOptions.UseHttps("danroth27.com", StoreName.My, StoreLocation.CurrentUser);
                    });
                })
                .UseContentRoot(Directory.GetCurrentDirectory())
                .UseIISIntegration()
                .ConfigureAppConfiguration((context, configBuilder) => {
                    configBuilder
                        .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
                        .AddJsonFile($"appsettings.{context.HostingEnvironment.EnvironmentName}.json", optional: true);

                    if (context.HostingEnvironment.IsDevelopment())
                    {
                        // For more details on using the user secret store see https://go.microsoft.com/fwlink/?LinkID=532709
                        configBuilder.AddUserSecrets<Startup>();
                    }

                    configBuilder.AddEnvironmentVariables();
                })
                .ConfigureLogging(loggerFactory => loggerFactory
                    .AddConsole()
                    .AddDebug())
                .UseStartup<Startup>()
                .Build();

            host.Run();
        }
    }

    public static class KestrelServerOptionsHttpsX509StoreExtensions
    {
        public static ListenOptions UseHttps(
            this ListenOptions options,
            string subjectName,
            StoreName storeName,
            StoreLocation storeLocation)
        {
            var certStore = new X509Store(storeName, storeLocation);
            certStore.Open(OpenFlags.ReadOnly);
            var certificates = certStore.Certificates
                .Find(X509FindType.FindBySubjectName, subjectName, validOnly: false);

            var certificate = certificates.OfType<X509Certificate2>().First();

            return options.UseHttps(certificate);
        }
    }
}
@danroth27
Copy link
Member Author

@Tratcher

@Tratcher
Copy link
Member

@halter73 @JunTaoLuo @natemcmaster From a quick debugging on Dan's machine, it looks like PreferHostingUrls isn't working as expected.

@davidfowl
Copy link
Member

We have a test for it right?

@Tratcher
Copy link
Member

The ordering in Hosting is wrong. UseIISIntegration sets the option in ConfigureServices (or HostingStartupAssemblies), but WebHost reads the option before that. The fix is to read the option directly from config rather than via options, like we do for the urls.

@Tratcher
Copy link
Member

@JunTaoLuo Can you file a new bug in Hosting, and add an integration test in this repo?

@muratg
Copy link
Contributor

muratg commented Jun 26, 2017

Closing as this was done in preview1.

@muratg muratg closed this as completed Jun 26, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants