Skip to content

Instantly share code, notes, and snippets.

@ayende
Created January 23, 2018 04:16
Show Gist options
  • Save ayende/d98efe53a67b03b8497078e5f9f73577 to your computer and use it in GitHub Desktop.
Save ayende/d98efe53a67b03b8497078e5f9f73577 to your computer and use it in GitHub Desktop.
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
namespace WebApplication1
{
public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args) =>
new WebHostBuilder()
.UseKestrel()
.ConfigureLogging((ctx, logging) =>
{
logging.SetMinimumLevel(LogLevel.None);
})
.UseStartup<Startup>()
.Build();
}
public class Startup
{
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.Run(async (context) =>
{
if (context.Request.Headers.TryGetValue("X-Forwarded-For", out var forward) &&
forward.Count != 0)
{
await context.Response.WriteAsync(forward[0]);
}
else
{
await context.Response.WriteAsync(context.Connection.RemoteIpAddress.ToString());
}
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment