示例#1
0
        private bool ValidateHost(Uri url)
        {
            var hosts = _config
                        .Configuration
                        .LocalNetworkAddresses
                        .Select(NormalizeConfiguredLocalAddress)
                        .ToList();

            if (hosts.Count == 0)
            {
                return(true);
            }

            var host = url.Host ?? string.Empty;

            _logger.Debug("Validating host {0}", host);

            if (_networkManager.IsInPrivateAddressSpace(host))
            {
                hosts.Add("localhost");
                hosts.Add("127.0.0.1");

                return(hosts.Any(i => host.IndexOf(i, StringComparison.OrdinalIgnoreCase) != -1));
            }

            return(true);
        }
示例#2
0
        /// <summary>
        /// Executes the middleware action.
        /// </summary>
        /// <param name="httpContext">The current HTTP context.</param>
        /// <param name="networkManager">The network manager.</param>
        /// <param name="serverConfigurationManager">The server configuration manager.</param>
        /// <returns>The async task.</returns>
        public async Task Invoke(HttpContext httpContext, INetworkManager networkManager, IServerConfigurationManager serverConfigurationManager)
        {
            var currentHost = httpContext.Request.Host.ToString();
            var hosts       = serverConfigurationManager
                              .Configuration
                              .LocalNetworkAddresses
                              .Select(NormalizeConfiguredLocalAddress)
                              .ToList();

            if (hosts.Count == 0)
            {
                await _next(httpContext).ConfigureAwait(false);

                return;
            }

            currentHost ??= string.Empty;

            if (networkManager.IsInPrivateAddressSpace(currentHost))
            {
                hosts.Add("localhost");
                hosts.Add("127.0.0.1");

                if (hosts.All(i => currentHost.IndexOf(i, StringComparison.OrdinalIgnoreCase) == -1))
                {
                    return;
                }
            }

            await _next(httpContext).ConfigureAwait(false);
        }