17

I m using ASP.Net core 2.0 web app and it is deployed on Azure. What I need to do is to get client IP Address. For this, I m searching all over the internet and found that the server variables help me on this.

So I found this code from here to get Client IP using:

string IpAddress = this.Request.ServerVariables["REMOTE_ADDR"];

But when I'm trying above code it shows me an error "HttpRequest does not contain a definition for Server Variables"

Also I was try this code:

var ip0 = HttpContext.Features.Get<IHttpConnectionFeature>()?.RemoteIpAddress;

Code Definition

RemoteIpAddress The IP Address of the client making the request. Note this may be for a proxy rather than the end user.

Above code is getting the IP address but it is not a clientip and each time when I access above code via controller it refreshes the IP. Maybe this is an Azure web service proxy which makes get request each time.

What is the right way to access server variables in ASP.Net Core 2.x?

Community
  • 1
  • 1
Ahmer Ali Ahsan
  • 5,636
  • 16
  • 44
  • 81
  • 1
    Have you tried request.HttpContext.Connection.RemoteIpAddress? – Mukesh Modhvadiya Jul 18 '18 at 06:18
  • 3
    Also see [Forwarded headers](https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/proxy-load-balancer#forwarded-headers) middleware. – Mark G Jul 18 '18 at 20:03
  • @MukeshModhvadiya yes I've tried but its returning me null at that time but after a brief explanation given in Mark G comment I've change my middleware so now I'm accessing the IP Address. Also I've posted my answer have a look. – Ahmer Ali Ahsan Jul 19 '18 at 07:02
  • @MukeshModhvadiya I appreciate your time and comment. – Ahmer Ali Ahsan Jul 19 '18 at 07:02

2 Answers2

13

I've found Mark G's reference link very useful.

I've configure the middleware with ForwardedHeadersOptions to forward the X-Forwarded-For and X-Forwarded-Proto headers in Startup.ConfigureServices.

Here is my Startup.cs code file:

ConfigureServices

public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<ApplicationDbContext>(options =>
           options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

    services.AddIdentity<ApplicationUser, IdentityRole>()
            .AddEntityFrameworkStores<ApplicationDbContext>()
            .AddDefaultTokenProviders();

    services.AddIdentityServer()
            .AddDeveloperSigningCredential()
            .AddInMemoryPersistedGrants()
            .AddInMemoryIdentityResources(Config.GetIdentityResources())
            .AddInMemoryApiResources(Config.GetApiResources())
            .AddInMemoryClients(Config.GetClients())
            .AddAspNetIdentity<ApplicationUser>();

    services.AddCors(options =>
    {
        options.AddPolicy("AllowClient",
                   builder => builder.WithOrigins("http://**.asyncsol.com", "http://*.asyncsol.com", "http://localhost:10761", "https://localhost:44335")
                                  .AllowAnyHeader()
                                  .AllowAnyMethod());
    });

    services.AddMvc();
    /* The relevant part for Forwarded Headers */
    services.Configure<ForwardedHeadersOptions>(options =>
    {
        options.ForwardedHeaders =
            ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
    });

    services.AddAuthentication(options =>
    {
        options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
        options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
    })
    .AddJwtBearer(options =>
    {
        // base-address of your identityserver
        //options.Authority = "http://server.asyncsol.com/";
        options.Authority = "http://localhost:52718/";

        // name of the API resource
        options.Audience = "api1";

        options.RequireHttpsMetadata = false;
    });
}

Configure

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    /* The relevant part for Forwarded Headers */
    app.UseForwardedHeaders();
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    app.UseIdentityServer();
    app.UseAuthentication();
    app.UseCors("AllowAll");
    app.UseMvc(routes =>
    {
        routes.MapRoute(
            name: "areas",
            template: "{area:exists}/{controller=Home}/{action=Index}/{id?}"
        );
        routes.MapRoute(
            name: "default",
            template: "{controller=Home}/{action=Index}/{id?}");
    });
}

Usage at a Controller

public IEnumerable<string> Get()
{
    string ip = Response.HttpContext.Connection.RemoteIpAddress.ToString();

    //https://en.wikipedia.org/wiki/Localhost
    //127.0.0.1    localhost
    //::1          localhost
    if (ip == "::1")
    {
        ip = Dns.GetHostEntry(Dns.GetHostName()).AddressList[2].ToString();
    }

    return new string[] { ip.ToString() };
}

So, If I'm running on my localhost environment it shows my IPv4 system IP Address.
If I'm running my server on azure it shows my Host Name / IP Address.

Conclusion:

I've found my answer in Mark G comment Forwarded Headers Middleware

Peter Csala
  • 17,736
  • 16
  • 35
  • 75
Ahmer Ali Ahsan
  • 5,636
  • 16
  • 44
  • 81
6

You can use HttpContext.Connection to get information about the connection (IP etc)

https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.http.httpcontext.connection?view=aspnetcore-2.1

https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.http.connectioninfo.remoteipaddress?view=aspnetcore-2.1#Microsoft_AspNetCore_Http_ConnectionInfo_RemoteIpAddress

davidfowl
  • 37,120
  • 7
  • 93
  • 103
  • I've already tried some code from your references. Like, if I want to know my server ip address through dns I have used this code `IPHostEntry heserver = Dns.GetHostEntry(Dns.GetHostName()); var ip = heserver.AddressList[2].ToString();` I dnt clear either this code returns client or server ip but when I deploy my web app on azure then on this line of code my app shows 500 internal error. And if I remove this line of code my web app run fine. I think dns is not working on azure anymore. Meanwhile if I run my question code its shows me new ip each time when I make a req to it. – Ahmer Ali Ahsan Jul 18 '18 at 20:29
  • I hope you understand. – Ahmer Ali Ahsan Jul 18 '18 at 20:29