Skip to content

Instantly share code, notes, and snippets.

@ayende ayende/ipiify.cs
Created Jan 23, 2018

Embed
What would you like to do?
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
You can’t perform that action at this time.