MVC4 Web Api Client IP - c#

i'm looking for a way to get the client ip address inside controller. I've tried HttpContext.Current.Request.UserHostAddress. This returns me ::1. Right now the Webserver is running on localhost and the requst is from localhost, too. Will this method work after deploying ?

Yes it will work.Now it is returing localhost in IP 6 format.
There are only a few things you should remember about:
If user is behind NAT or proxy it will show IP of NAT/proxy
There is a discussion about more problems with wrong IP UserHostAddress gives wrong IPs

Related

Getting true client IP address using C# in ASP.NET MVC

I have an ASP.NET MVC web app and I need to get the client IP address.
I researched this and there are two main possibilities:
to retrieve the IP using HTTP headers
to retrieve it using HttpContext.Request.UserHostAddress
The problem with the first method is that the IP can easily be spoofed.
The problem with the second method is that if the client is behind a proxy, it retrieves the client proxy - not the client's own IP.
(See here - this is the best resource I found in SO describing the above problem)
My question: how can I retrieve the client's IP address in a way that cannot be spoofed and to get his own IP address, not his proxy's IP - is it possible?

Get Client's Mac address at server side in WCF service C# Not a duplicate of (Obtaining client IP address in WCF 3.0)

I have WCF service hosted as windows service, I am trying to get clients (WPF client) Mac address at server end (WCF side). I have tried using following line of code but it gives me IP address of client that too in ::1 format. RemoteEndpointMessageProperty prop = OperationContext.Current.IncomingMessageProperties[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
prop.Address is ::1
How can I get client Mac address so that I can differentiate among the client hitting my service. There could be scenarios where clients under same network may give me same IP address and that is reason I am looking for Mac address to identity client's call..
This question got closed and marked duplicate however its not something related to IP address. I am looking for Mac address!!! Please let answers come and don't close this!!!
You can only get MAC address on the same subnet as the sever so it will not be more unique than client IP.
There is no dotnet API to get the address within the same subnet but if you really want you can try running an external program and captiapturr the output, see options listed
https://superuser.com/questions/188799/how-to-find-the-mac-address-of-a-remote-computer

Why is the uri for a self hosting application set as localhost?

I recently started looking into Asp.net Web Api self hosting. I'm following this article and I have two questions based on that.
Why is the URI for HttpSelfHostConfiguration pointing to localhost?
How would I give a website name like I would in IIS for the windows service? Because if I deploy this to a production server, how would the clients call the localhost?
Q. Why is the URI for HttpSelfHostConfiguration pointing to localhost?
A. localhost simply just means "this computer". It's typical to do it this way to make the intent clear. Nothing stops you from using an ip address.
Q. How would I give a website name like I would in IIS for the windows service? Because if I deploy this to a production server, how would the clients call the localhost?
A. You would change from HttpSelfHostConfiguration("http://localhost:80") to HttpSelfHostConfiguration("http://000.000.000:80") where 000.000.000 is the ip address of the host (the machine you are running the web application on)
Localhost is reserved loopback adres (ea 127.0.0.1) try ping localhost.
The origin is basic networking, it existed before .net
Pinging localhost was quite common to check if your own network card still worked. As it ping 127.0.0.1, is a ping to your own network card. Next step in conectivity issues was pinging your gateway and then some remote IP or so.
A client cannot connect to a remote localhost (it will point its own network card).
in regard to the article, a server listens to its own network card.
unless you have some multiple nic environment with specific routes to follow
but then there is a config file for that.
Localhost is the default allocation of Windows, if you want to change the default name binding,
answer is simple just go to
C:\Windows\System32\drivers\etc\ folder
you can find as hosts file there just open it in notepad, if you want to change the locally hosted name add a new line in that text file like
127.0.0.1 domain name you want to use
for example
127.0.0.1 techdoubts.net
then your localhost will be changed to techdoubts.net
and one more, add that new name in internet information services website binding also.

Cannot connect to WebSocketServer using IP (but localhost is ok)

This is very straightforward. I have a WebSocketServer project that works great if the uri is http://localhost:8081/ but if I use the local IP instead of localhost http://10.0.0.201:8081/, it refuses to answer requests on that IP. I am sure its a permissions issue or unable to access the port issue but I am, frankly, not getting any errors. It just simply doesn't respond. Can anyone shed some light on this? This is all in c#.
Again this is a SERVER. It is supposed to LISTEN on 10.0.0.201 (and, of course, localhost). Localhost works, but the IP does not from any client.
I am intentionally not posting code (although I could if necessary) because this is a multi-threaded application that supports multiple concurrent clients. And it works flawlessly through localhost. I am only changing the word "localhost" to the machine's IP. The failure feels like a binding or permissions issue.
This is probably due to Http Binding in your webserver (e.g. IIS), the binding could be explicitly requiring localhost and if the URI does not have localhost then it would refuse this connection.
Try changing the binding to empty hostname and URI should be allowed.
This is a suggestion, not an answer. Have you tried hitting the WebServer from a different machine? Even if it's just a static test page?
It could be a perms problem, but this would at least tell you if any machine on the local network can get to it using the IP/Port configuration.
Try binding to *
If using HttpListener try this:
httpListener = new HttpListener();
httpListener.Prefixes.Add("http://*/");
instead of
httpListener.Prefixes.Add("http://localhost/");
httpListener.Prefixes.Add("http://10.0.0.201/");
Binding to localhost will not bind to all the rest, and binding to a specific IP address will not work with localhost or IPv6 for example.

calling a route with specific ip address

For the project I'm working on, I need to directly call the IP address of a computer within our C# environment. We are using asp.net / mvc 3 and the customer facing website has a controller that will expose an endpoint to hit from within the admin area of the site. This controller action will then call another private internal ip address that actually does the ground work. Basically there are two machines, one is customer facing, and the other machine is more for setup.
<private_ip_address>/admin/do_something_private
Does C# have a direct way to call an IP address like this?
As long as the PCs can see each other you can always establish a communication between them via HTTP. Just make sure you know how the endpoint is configured and what kind of "requests" it understands. Yet, the client must be aware of any security implementation on the server side in order to successfully authenticate the request. That's as abstract as I can get provided that your question is way too abstract
Your internal machine will have an external IP address. You'll need to point your external
L website to the external IP address of your modem/router and use your modem/router to route the request to the internal IP address. You can also put your internal machine on your router's DMZ but that's usually a bad idea.

Categories

Resources