This repository has been archived by the owner on Dec 20, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 871
/
KestrelServerOptionsSetup.cs
109 lines (95 loc) · 4.63 KB
/
KestrelServerOptionsSetup.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
namespace Microsoft.AspNetCore
{
internal class KestrelServerOptionsSetup : IConfigureOptions<KestrelServerOptions>
{
private const string DefaultCertificateSubjectName = "CN=localhost";
private const string DevelopmentSSLCertificateName = "localhost";
private readonly IHostingEnvironment _hostingEnvironment;
private readonly IConfiguration _configurationRoot;
private readonly ILoggerFactory _loggerFactory;
public KestrelServerOptionsSetup(
IHostingEnvironment hostingEnvironment,
IConfiguration configurationRoot,
ILoggerFactory loggerFactory)
{
_hostingEnvironment = hostingEnvironment;
_configurationRoot = configurationRoot;
_loggerFactory = loggerFactory;
}
public void Configure(KestrelServerOptions options)
{
BindConfiguration(options);
}
private void BindConfiguration(KestrelServerOptions options)
{
var certificateLoader = new CertificateLoader(_configurationRoot.GetSection("Certificates"), _loggerFactory, _hostingEnvironment.EnvironmentName);
foreach (var endPoint in _configurationRoot.GetSection("Kestrel:EndPoints").GetChildren())
{
BindEndPoint(options, endPoint, certificateLoader);
}
}
private void BindEndPoint(
KestrelServerOptions options,
IConfigurationSection endPoint,
CertificateLoader certificateLoader)
{
var configAddress = endPoint.GetValue<string>("Address");
var configPort = endPoint.GetValue<string>("Port");
if (!IPAddress.TryParse(configAddress, out var address))
{
throw new InvalidOperationException($"Invalid IP address in configuration: {configAddress}");
}
if (!int.TryParse(configPort, out var port))
{
throw new InvalidOperationException($"Invalid port in configuration: {configPort}");
}
options.Listen(address, port, listenOptions =>
{
var certificateConfig = endPoint.GetSection("Certificate");
X509Certificate2 certificate = null;
if (certificateConfig.Exists())
{
try
{
try
{
certificate = certificateLoader.Load(certificateConfig).FirstOrDefault();
}
catch (KeyNotFoundException) when (certificateConfig.Value.Equals(DevelopmentSSLCertificateName, StringComparison.Ordinal) && _hostingEnvironment.IsDevelopment())
{
var storeLoader = new CertificateStoreLoader();
certificate = storeLoader.Load(DefaultCertificateSubjectName, "My", StoreLocation.CurrentUser, validOnly: false) ??
storeLoader.Load(DefaultCertificateSubjectName, "My", StoreLocation.LocalMachine, validOnly: false);
if (certificate == null)
{
var logger = _loggerFactory.CreateLogger("Microsoft.AspNetCore.KestrelOptionsSetup");
logger.LogError("No HTTPS certificate was found for development. For information on configuring HTTPS see https://go.microsoft.com/fwlink/?linkid=848054.");
}
}
if (certificate == null)
{
throw new InvalidOperationException($"No certificate found for endpoint '{endPoint.Key}'.");
}
}
catch (Exception ex)
{
throw new InvalidOperationException("Unable to configure HTTPS endpoint. For information on configuring HTTPS see https://go.microsoft.com/fwlink/?linkid=848054.", ex);
}
listenOptions.UseHttps(certificate);
}
});
}
}
}