예제 #1
0
        /// <summary>
        /// configure Kestrel to Host
        /// </summary>
        /// <param name="options"></param>
        internal static void SetHost(Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServerOptions options)
        {
            var configuration = (IConfiguration)options.ApplicationServices.GetService(typeof(IConfiguration));
            var host          = configuration.GetSection("RafHost").Get <Host>();  //依据Host类反序列化appsettings.json中指定节点

            foreach (var endpointKvp in host.Endpoints)
            {
                var endpointName = endpointKvp.Key;
                var endpoint     = endpointKvp.Value;            //获取appsettings.json的相关配置信息
                if (!endpoint.IsEnabled)
                {
                    continue;
                }

                var address = IPAddress.Parse(endpoint.Address);
                options.Listen(address, endpoint.Port, opt =>
                {
                    if (endpoint.Certificate != null)                    //证书不为空使用UserHttps
                    {
                        switch (endpoint.Certificate.Source)
                        {
                        case "File":
                            opt.UseHttps(endpoint.Certificate.Path, endpoint.Certificate.Password);
                            break;

                        default:
                            throw new NotImplementedException($"文件 {endpoint.Certificate.Source}还没有实现");
                        }
                        //opt.UseConnectionLogging();
                    }
                });

                options.UseSystemd();
            }
        }
예제 #2
0
        private static void SetHost(Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServerOptions options)
        {
            var configuration = (IConfiguration)options.ApplicationServices.GetService(typeof(IConfiguration));
            var host          = configuration.GetSection("RafHost").Get <Host>();

            foreach (var endpointKvp in host.Endpoints)
            {
                var endpointName = endpointKvp.Key;
                var endpoint     = endpointKvp.Value;
                if (!endpoint.IsEnabled)
                {
                    continue;
                }

                var address = IPAddress.Parse(endpoint.Address);
                options.Listen(address, endpoint.Port, opt =>
                {
                    if (endpoint.Certificate != null)
                    {
                        switch (endpoint.Certificate.Source)
                        {
                        case "File":
                            opt.UseHttps(endpoint.Certificate.Path, endpoint.Certificate.Password);
                            break;

                        default:
                            throw new NotImplementedException($"The source {endpoint.Certificate.Source} is not yet implemented");
                        }
                    }
                });

                options.UseSystemd();
            }
        }
예제 #3
0
        private static void SetHost(Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServerOptions options)
        {
            var configuration = (IConfiguration)options.ApplicationServices.GetService(typeof(IConfiguration));
            var host          = configuration.GetSection("RafHost").Get <Host>();

            foreach (var endpointKvp in host.Endpoints)
            {
                var endpointName = endpointKvp.Key;
                var endpoint     = endpointKvp.Value;
                if (!endpoint.IsEnabled)
                {
                    continue;
                }

                var address = IPAddress.Parse(endpoint.Address);
                options.Listen(address, endpoint.Port, opt =>
                {
                    if (endpoint.Certificate != null)
                    {
                        switch (endpoint.Certificate.Source)
                        {
                        case "File":
                            opt.UseHttps(endpoint.Certificate.Path, endpoint.Certificate.Password);
                            break;

                        case "Store":
                            using (var store = new X509Store(StoreName.My, StoreLocation.LocalMachine))
                            {
                                store.Open(OpenFlags.ReadOnly);
                                var certs = store.Certificates.Find(X509FindType.FindBySubjectName, "localhost", false);
                                if (certs.Count > 0)
                                {
                                    var certificate = certs[0];
                                    opt.UseHttps(certificate);
                                }
                            }
                            break;

                        default:
                            throw new NotImplementedException($"The source {endpoint.Certificate.Source} is not yet implemented");
                        }

                        //opt.UseConnectionLogging();
                    }
                });

                options.UseSystemd();   //?
            }
        }
예제 #4
0
        /// <summary>
        /// 配置Kestrel
        /// </summary>
        /// <param name="options"></param>
        private static void SetHostUrl(Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServerOptions options)
        {
            //依据Host类反序列化appsettings.json中指定节点
            var hostUrl = ConfigurationJson.HostUrl;

            foreach (var endpointKvp in hostUrl.Endpoints)
            {
                var endpointName = endpointKvp.Key;
                var endpoint     = endpointKvp.Value;//获取appsettings.json的相关配置信息
                if (!endpoint.IsEnabled)
                {
                    continue;
                }

                var address = System.Net.IPAddress.Parse(endpoint.Address);
                options.Listen(address, endpoint.Port, opt =>
                {
                    if (endpoint.Certificate != null)//证书不为空使用UserHttps
                    {
                        if (endpoint.Certificate.Source != "File" || File.Exists(endpoint.Certificate.Path))
                        {
                            switch (endpoint.Certificate.Source)
                            {
                            case "File":
                                opt.UseHttps(endpoint.Certificate.Path, endpoint.Certificate.Password);
                                break;

                            default:
                                throw new NotImplementedException($"文件 {endpoint.Certificate.Source}还没有实现");
                            }
                            //opt.UseConnectionLogging();
                        }
                    }
                });

                options.UseSystemd();
            }
        }