Created
January 23, 2018 04:16
-
-
Save ayende/d98efe53a67b03b8497078e5f9f73577 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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