c# - Getting 127.0.0.1 when using HttpContext.Features.Get<IHttpConnectionFeature>()?.RemoteIpAddress -


i using asp.net core mvc. trying ip address using below code.

var ipaddress = httpcontext.features.get<ihttpconnectionfeature>()?.remoteipaddress?.tostring(); 

its returning ::1 means 127.0.0.1 on local machine fine. have hosted on azure cloud testing using testing azure account , still giving me 127.0.0.1.

what doing wrong?

project.json

    {   "dependencies": {     "microsoft.netcore.app": {       "version": "1.0.0-rc2-3002702",       "author": [ "musaab mushtaq", "programmer" ],       "type": "platform"     },     "microsoft.aspnetcore.server.iisintegration": "1.0.0-rc2-final",     "microsoft.aspnetcore.server.kestrel": "1.0.0-rc2-final",     "microsoft.aspnetcore.mvc": "1.0.0-rc2-final",     "microsoft.aspnetcore.staticfiles": "1.0.0-rc2-final",     "microsoft.aspnetcore.diagnostics": "1.0.0-rc2-final",     "microsoft.aspnet.mvc.taghelpers": "6.0.0-rc1-final",     "microsoft.aspnetcore.mvc.viewfeatures": "1.0.0-rc2-final",     "developerforce.force": "1.3.0",     "microsoft.framework.configuration": "1.0.0-beta8",     "microsoft.extensions.configuration.json": "1.0.0-rc2-final",     "microsoft.visualstudio.web.browserlink.loader": "14.0.0-rc2-final",     "integrapay.domain": "1.0.0-*"   },    "tools": {     "microsoft.aspnetcore.server.iisintegration.tools": {       "version": "1.0.0-preview1-final",       "imports": "portable-net45+win8+dnxcore50"     }   },    "frameworks": {     "netcoreapp1.0": {       "imports": [         "dotnet5.6",         "dnxcore50",         "portable-net45+win8",         "net45"       ],       "dependencies": {       }     }   },    "buildoptions": {     "emitentrypoint": true,     "preservecompilationcontext": true   },    "runtimeoptions": {     "gcserver": true   },    "publishoptions": {     "include": [       "wwwroot",       "web.config",       "config.json",       "views"     ]   },    "scripts": {     "postpublish": [ "dotnet publish-iis --publish-folder %publish:outputpath% --framework %publish:fulltargetframework%" ]   } } 

startup.cs

  public class startup     {         // method gets called runtime. use method add services container.         // more information on how configure application, visit http://go.microsoft.com/fwlink/?linkid=398940         public void configureservices(iservicecollection services)         {             services.configure<forwardedheadersoptions>(option => { option.forwardedheaders = microsoft.aspnetcore.httpoverrides.forwardedheaders.xforward‌​edfor; });             services.addmvc();             services.addsingleton(provider => configuration);             services.addtransient<iregistrationrepository, serviceutilities>();             services.addtransient<iclientserviceconnector, clientservicevalidation>();         }          private iclientserviceconnector forceclientservice { get;set; }         private iconfiguration configuration { get; set; }         // method gets called runtime. use method configure http request pipeline.         public void configure(iapplicationbuilder app, ihostingenvironment enviroment)         {             app.usestaticfiles();             if (enviroment.isdevelopment())             {                 app.usedeveloperexceptionpage();             }             else             {                 app.useexceptionhandler("/registration/error");             }              app.useruntimeinfopage("/info");             app.usefileserver();             configurerestauthenticationsetting(enviroment);             app.usemvc(configureroutes);             app.run(async (context) =>             {                 await context.response.writeasync("hello world!");             });         }          private void configurerestauthenticationsetting(ihostingenvironment enviroment)         {             var config = new configurationbuilder()                 .setbasepath(enviroment.contentrootpath)                 .addjsonfile("config.json");             configuration = config.build();         }          private void configureroutes(iroutebuilder routebuilder)         {             routebuilder.maproute("default", "{controller=registration}/{action=index}/{formid?}");         }       } 

this happen because of reverse proxy. asp.net core iis send request kestrel server processing , mvc 6 ( asp.net core) receive request after issue come header information not forwarded.

following thing solve problem.

in startup.cs file.

public void configure(iapplicationbuilder app, ihostingenvironment env, iloggerfactory loggerfactory)     {       loggerfactory.addconsole(configuration.getsection("logging"));       loggerfactory.adddebug();       app.useforwardedheaders(new forwardedheadersoptions() { forwardedheaders = microsoft.aspnetcore.httpoverrides.forwardedheaders.all }); // must first line. ( before other middleware configured)       // other code     } 

update 1 1. don't use service.configure , app.useforwardedheaders together. ( have tried use both option , ended 127.0.0.1). 2. have use app.useforwardedheaders , works fine.

my minimal configuration file. ( startup.cs)

public class startup {     public void configureservices(iservicecollection services)     {          services.addmvc();         //services.configure<forwardedheadersoptions>(option => { option.forwardedheaders = microsoft.aspnetcore.httpoverrides.forwardedheaders.all; });  // option should not used. gives me 127.0.0.1 if have used option.     }      public void configure(iapplicationbuilder app, ihostingenvironment env, iloggerfactory loggerfactory)     {         loggerfactory.addconsole(loglevel.debug);         app.useforwardedheaders(new forwardedheadersoptions() { forwardedheaders = microsoft.aspnetcore.httpoverrides.forwardedheaders.all });         app.usestaticfiles();          if (env.isdevelopment())         {             app.usedeveloperexceptionpage();         }          app.usemvc();     } } 

update 2

later tried service.configure , works. in case have use service.configure , avoid using app.useforwardedheaders.

in case startup.cs file this.

public class startup {     public void configureservices(iservicecollection services)     {          services.addmvc();         services.configure<forwardedheadersoptions>(option => { option.forwardedheaders = microsoft.aspnetcore.httpoverrides.forwardedheaders.xforward‌​edfor; });       }      public void configure(iapplicationbuilder app, ihostingenvironment env, iloggerfactory loggerfactory)     {         loggerfactory.addconsole(loglevel.debug);         //app.useforwardedheaders(new forwardedheadersoptions() { forwardedheaders = microsoft.aspnetcore.httpoverrides.forwardedheaders.all });         app.usestaticfiles();          if (env.isdevelopment())         {             app.usedeveloperexceptionpage();         }          app.usemvc();     } } 

Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -