I try to get remote (client) IP addres:
var ip = httpContext.Features.Get<IHttpConnectionFeature>()?.RemoteIpAddress
But it works only for local requests (it will return ::1 value)
When I load page from remote machine the value is null. I investigated there is no IHttpConnectionFeature in the Features collection in this case.
Why? And how to get remote ip address correctly?
I know that this post is old but I came here looking for the same question and finnaly I did this:
On project.json add dependency:
"Microsoft.AspNetCore.HttpOverrides": "1.0.0"
On Startup.cs, in the Configure method add:
app.UseForwardedHeaders(new ForwardedHeadersOptions
{
ForwardedHeaders = ForwardedHeaders.XForwardedFor |
ForwardedHeaders.XForwardedProto
});
And, of course:
using Microsoft.AspNetCore.HttpOverrides;
Then, I got the ip like this:
Request.HttpContext.Connection.RemoteIpAddress
In my case, when debugging in VS I got always IpV6 localhost, but when deployed on an IIS I got always the remote IP.
Some useful links:
How do I get client IP address in ASP.NET CORE? and RemoteIpAddress is always null
The ::1 may be because:
Connections termination at IIS, which then forwards to Kestrel, the v.next web server, so connections to the web server are indeed from localhost.
(https://stackoverflow.com/a/35442401/5326387)
Just try this:
var ipAddress = HttpContext.Connection.RemoteIpAddress;
And if you have another computer in same LAN, try to connect with this pc but use user ip instead of localhost. Otherwise you will get always ::1 result.
Related
I have a Servlet project developed using Tomcat 7.
My projects logic is that whenever a request comes from certain IP it responds to the same IP with a new instance of browser as an response.
When I run and test this application locally it works perfect and by the way my local working IP address will be something like this 10.52.xxx.xxx. So request has 10.52.xxx.xxx and response is given to 10.52.xxx.xxx and all happies
Now things looks perfect and I planned to deploy this project in a Server which has an IP addess of 172.32.xxx.xxx and I have completed deployment of the same.
Now the issue which I am facing is that when I try to make a request to the project in the server (172.32.xxx.xxx) from my network (10.52.xxx.xxx).
In my project I have code like
String ip = request.getRemoteAddr(); //gets request IP address to give a new instance of browser
Now the variable ip contains an IP address of 172.32.xxx.xxx. So my code will try to give a browser instance to 172.32.xxx.xxx which doesn't exist. It should ideally give the browser instance back to 10.52.xxx.xxx as this is from which it had received the request so it has to respond to this IP.
I know both Server and the client request are in different IP network but I would like to know all possible solutions to fix the same so that browser is given back to 10.52.xxx.xxx.
Any help highly appreciated.
I have successfully setup IIS on my local development machine (dev branch - setup as localdev.me) but when I went to setup another branch (hotfix - setup as localhotfix.me) I am running into issues. The issues are due to the way the site is setup. The subdomain of the url is used to determine which Database to connect to. So going to host.localdev.me will connect to the host database. So in IIS I have the following settings for the bindings of the site.
Type Host Name Port IP Address
http localdev.me 80 *
http *.localdev.me 80 *
I can ping localdev.me with any subdomain and I get the loopback address as expected. When I then setup the hotfix branch (exactly the same as the dev but with the following bindings) I get name not resolved errors.
Type Host Name Port IP Address
http localhotfix.me 80 *
http *.localhotfix.me 80 *
Is there a reason the first setup would work and not the second? What is perhaps even stranger if I tell IIS to stop I can still ping subdomains on localdev.me and get the loopback address.
I could always get it working by manually specifying the host name in my windows hosts file but I would rather not do that as I would need to go in and edit the file every time we add a new subdomain.
EDIT: These are the specific errors I am getting.
ping localhotfix.me
Ping request could not find host localhotfix.me. Please check the name and try again.
EDIT2: I have a solution that works fairly well. It requires Acrylic DNS and installation of the Microsoft Loopback Adapter. I set the loopback adapter to a valid IP Address and set the DNS server to 127.0.0.1 then edit the AcrylicHosts file to contain entries for each domain with a wildcard. Once I did all of this I was able to ping localhotfix.me along with *.localhotfix.me. I believe the reason localdev.me worked is because it is a valid domain. The name would resolve at which point I believe IIS was able to take over. But thats really just an educated guess. But kindof makes sense as to why it worked for one and not the other.
The reason *.localdev.me works without a hosts file is because the public DNS for that domain resolves to 127.0.0.1 as long as it is not localdev.me or www.localdev.me. You can check this using nslookup *.localdev.me (replace the asterisk with anything except www) while your hosts file is empty. On the other hand, *.localhotfix.me is not registered in public DNS at all, which is why you'd need a hosts file entry for those.
I've written a tool that validates emails, mostly copied from various examples on the subject around on the internet (user Don Worthley compiled a decent collection of code samples here https://delicious.com/dworthley/email.validation). Unfortunately after creating the socket and the IPEndPoint from the email to verify's hostname IP and port, my code always fails to connect to the socket due to timeout.
Here is the pertinent code (fails at s.Connect(endPt) due to timeout):
private bool smtpCheckEmail(string email)
{
try
{
string[] host = (email.Split('#'));
string hostname = host[1];
IPHostEntry IPhst = Dns.GetHostEntry(hostname);
IPEndPoint endPt = new IPEndPoint(IPhst.AddressList[0], 25);
using (Socket s = new Socket(endPt.AddressFamily, SocketType.Stream, ProtocolType.Tcp))
{
try
{
s.Connect(endPt);
}
catch
{
s.Close();
return false;
}
From my research others have reported success with similar code so it might be an issue with my server host. The MVC project is on a GoDaddy VPS with Windows 7 and standard firewall/MSSE as protection (I've tested with both disabled with same timeout result).
I've verified I can telnet to my own host and run through the various HELO, Mail from etc commands but it also times out if I try to connect to smtp for other hosts.
Maybe it's an ISP issue?
I've also tried installing EmailVerify for .NET trial but run into the same problem where any validation at or above SMTP check times out.
Would be great to get some suggestions of what might be the problem and how to troubleshoot it.
Edit: After doing some comparison testing with one of my Azure VPS it looks like it is likely to be server related, "telnet 74.125.206.108 smtp" (gmail's smtp host IP) through command line connects on the Azure box but not on the GoDaddy one my project is on.
I also notice that when my code pulls the IP from the host name (Dns.GetHostEntry(hostname)), in this gmail example it returns 216.58.216.37 which matches what I get if a run nslookup in cmd for gmail.com, however the IP for smtp that I can telnet to is for gmail is 74.125.206.108 which matches the nslookup record for smtp.gmail.com. Could my code be grabbing the wrong IP addresses in many cases here?
Turned out to be a limitation of GoDaddy VPS. Transferred code to an Azure hosted project and it worked great.
I have a site (ASP.NET WebForms) hosted on IIS7. I have a static IP. My router is pointed at the right computer IP on my network. And the site works fine both on the server and on computers on the same network when I go to the interal IP of the server. However, when I try to access the site remotely via the static IP or the domain name, I get a 504 timeout error.
I have absolutely no idea where to begin, because the 504 error gives me no information as to what the problem is. Port 80, which is the port this is on, is open for inbound firewall rules... It wasn't for outbound, but I opened that to test it, and it still didn't work.
Other than that, I'm out of ideas. Any help is much appreciated!
It means you have deployed your web at your home. When you are trying to call at Intranet using the domain name or static IP address your are able to do so, but when your try to access on Internet you cant.
The solution which I can see for that is because on Internet, It dose not recognize your static IP address you need to pass your dynamic IP of your ISP.
Go here this will tell your IP information something like : 202.147.18.194 and instead of passing your static machine IP pass this IP when you are trying to access over internet. The URL of your web would be something like this:
"http://202.147.18.194/MyWeb/index.asp"
We have a website/webservice running on one of our servers. We hit that webservice once a day with a scheduled .bat scipt.
Just recently the scheduled task started to fail. The log shows "Invalid URI: The hostname could not be parsed."
When we're logged into the server we can reach the website through http://localhost/website, but get an Invalid URI error when we hit the webservice http://localhost/website/ws.asmx.
Any ideas?
Edit:
We'be tried using the servername, the ipaddress, and port numbers (and combinations therein). Any other ideas?
For the private IP Address your using Wikipedia Reference http://en.wikipedia.org/wiki/IP_address for what are the limits and proper uses.
If you have access to the source code for the web service look for fixed IP Address being used along with having correct Server Bindings for using the service.
Try using the IP Address of the machine instead of localhost.
Basically, I'm taking a wild stab that someone ran a lockdown or a service patch was installed that somehow is filtering request resposes to localhost (127.0.0.1).
Try using the machine name instead of localhost