예제 #1
0
        public TrustIPFilter
            (ILogger <TrustIPFilter> logger, IConfiguration configuration)
        {
            _logger = logger;

            IPHelper.InitByConfiguration(configuration, logger);
        }
        /// <summary>
        /// 启用授信IP
        /// </summary>
        /// <param name="app"></param>
        /// <param name="loggerFactory"></param>
        /// <param name="configuration"></param>
        /// <returns></returns>
        public static IApplicationBuilder UseTrustIP(this IApplicationBuilder app, ILoggerFactory loggerFactory, IConfiguration configuration)
        {
            if (app == null)
            {
                throw new ArgumentNullException(nameof(app));
            }

            var logger = loggerFactory.CreateLogger <TrustIPMiddleware>();

            IPHelper.InitByConfiguration(configuration, logger);
            return(app.UseMiddleware <TrustIPMiddleware>(logger));
        }
예제 #3
0
        public override void OnActionExecuting(ActionExecutingContext context)
        {
            _logger.LogInformation(
                $"IP: {context.HttpContext.Connection.RemoteIpAddress}");

            if (IPHelper.IsTrustIP(context.HttpContext.Connection.RemoteIpAddress))
            {
                base.OnActionExecuting(context);
                return;
            }
            context.Result = new StatusCodeResult(403);
        }
예제 #4
0
        public async Task Invoke(HttpContext context)
        {
            var remoteIp = context.Connection.RemoteIpAddress;

            _logger.LogInformation(
                $"IP: {remoteIp}");

            if (IPHelper.IsTrustIP(remoteIp))
            {
                await _next.Invoke(context);

                return;
            }
            context.Response.StatusCode = (int)HttpStatusCode.Forbidden;
        }