wcf client ip as ipv6 - c#

i'm using next piece of code to get client ip on wcf service :
OperationContext context = OperationContext.Current;
System.ServiceModel.Channels.MessageProperties prop = context.IncomingMessageProperties;
System.ServiceModel.Channels.RemoteEndpointMessageProperty endpoint = prop[System.ServiceModel.Channels.RemoteEndpointMessageProperty.Name] as System.ServiceModel.Channels.RemoteEndpointMessageProperty;
string ip = endpoint.Address;
while this code worked on iis6/server2003 everything were ok, endpoint.Address returned ipv4.
but after i recently updated to iis7/server2008 endpoint.Address is returning ipv6.
is it still possible to get ipv4 on iis7/server2008 ?
Thank You !

This is not so much of a change in WCF that is a change in networking. Your client has used its IPv6 to connect to the server and that is the address which is stored in the context of the message. If you need to get hold of IPv4, use the snippet below:
IPAddress ipAddress = IPAddress.Parse(ipv6);
IPHostEntry ipHostEntry = Dns.GetHostEntry(ipAddress);
foreach (IPAddress address in ipHostEntry.AddressList)
{
if(address.AddressFamily == AddressFamily.InterNetwork)
Console.WriteLine(address);
}
This will translate your IPv6 to IPv4.

Related

How can I get a client's local IP address, like 192.168.0.1?

I have already tried all solutions in Stack Overflow question How to get a user's client IP address in ASP.NET?. It gives me the server's IP address, not the client's LAN IP address.
I need to get client's local IP address, and it must be seen as 192.168.2.1, but I always get the server's IP address instead of it.
Here is my code:
System.Web.HttpContext context = System.Web.HttpContext.Current;
string ipAddress = context.Request.ServerVariables["LOCAL_ADDR"];
There is a website doing this, http://net.ipcalf.com/.
How do I solve this issue?
You want the remote address, not the local address
context.Request.ServerVariables["REMOTE_ADDR"]
Your code grabs the server IP address, not the client's. Try the following below:
This code was copied from Stack Overflow question How to get a user's client IP address in ASP.NET?.
protected string GetIPAddress()
{
System.Web.HttpContext context = System.Web.HttpContext.Current;
string ipAddress = context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (!string.IsNullOrEmpty(ipAddress))
{
string[] addresses = ipAddress.Split(',');
if (addresses.Length != 0)
{
return addresses[0];
}
}
return context.Request.ServerVariables["REMOTE_ADDR"];
}

foreach over DNS host addresses

I have this code:
IPHostEntry host = null;
Socket sock;
host = Dns.GetHostEntry("ip..");
foreach (IPAddress address in host.AddressList)
{
IPEndPoint ipe = new IPEndPoint(address, 7777);
sock = new Socket(ipe.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
sock.Connect(ipe);
if (sock.Connected)
{
sock.SendTo(Encoding.UTF8.GetBytes("Hello world"), ipe);
}
}
This code works ok on localhost, but when I write vps ip, code not working, what's problem?
It would seem your DNS is set up incorrectly and Dns.GetHostEntry(string) fails at the second point below. If the DNS server fails to do a reverse lookup it won't return a hostname, so Dns.GetHostEntry(string) doesn't know what to look up and will return an empty address list.
From MSDN: https://msdn.microsoft.com/en-us/library/ms143998.aspx
The method tries to parse the address. If the hostNameOrAddress parameter contains a legal IP string literal, then the first phase succeeds.
A reverse lookup using the IP address of the IP string literal is attempted to obtain the host name. This result is set as the HostName property.
The host name from this reverse lookup is used again to obtain all the possible IP addresses associated with the name and set as the AddressList property.

How to obtain local ip address

I've managed to get clients' ip addresses from my server using codes below
OperationContext context = OperationContext.Current;
MessageProperties prop = context.IncomingMessageProperties;
RemoteEndpointMessageProperty endpoint = prop[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
string ipaddress = endpoint.Address;
However, if clients are under the same network, all the ip addresses are public ip address of the network.
Is there a way to get local ip addresses of each client if they are under the same AP?
or just simply, is there a easier way to distinguish client from one another?
Because of network address translation it is not always possible to identify clients by unique client addresses see How to get a user's client IP address in ASP.NET?
try this
HttpRequest currentRequest = HttpContext.Current.Request;
string ipAddress = currentRequest.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (ipAddress == null || ipAddress.ToLower() == "unknown")
ipAddress = currentRequest.ServerVariables["REMOTE_ADDR"];
return ipAddress;
You can try webrtc's in built feature to connect to STUN server https://diafygi.github.io/webrtc-ips/

How to get LAN IP of a client using .net?

I want to get the adrress IP (LAN ip) of the computer which access my site.
How can i get it?
You can't.
Browsers don't send their local IPs in HTTP headers, so there is no way for you to get it. You only get the router's external (internet) IP.
Method 1:
You can get that by using below mentioned link.
List the IP Address of all computers connected to a single LAN
Method 2:
You can try below one also.
public string GetUserIP()
{
string ipList = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (!string.IsNullOrEmpty(ipList))
{
return ipList.Split(',')[0];
}
return Request.ServerVariables["REMOTE_ADDR"];
}
Method 3:
System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName()).AddressList.GetValue(0).ToString();
I hope this will help to you.
I assume you're using asp.net and in that case you could use Request.UserHostAddress to retrieve the IP address of a client.
Use this function:
public string GetLanIP()
{
IPHostEntry host;
string localIP = "?";
host = Dns.GetHostEntry(Dns.GetHostName());
foreach (IPAddress ip in host.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
localIP = ip.ToString();
}
}
return localIP;
}

get device ip address from service provider ip address

I need get the local machine ip address of my website visited users for that i used below code
string ipAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (!string.IsNullOrEmpty(ipAddress))
{
string[] addresses = ipAddress.Split(',');
if (addresses.Length != 0)
{
stradd = addresses[0];
}
else
{
stradd = ipAddress;
}
}
else
{
stradd = Request.ServerVariables["REMOTE_ADDR"].ToString();
}
hostName = Dns.GetHostByAddress(stradd).HostName;
this is giving the ip address of the service provider & name of the service provider but i don't want this i wanted user device(local) ip address, is it possible to get local ip address? please help me.
No. You can't get the private IP address of a machine hidden behind NAT.
The router relays the request transparently.
The local IP is just that (local) it will never leave the local router, you will need to use some sort of locally executed code to send it to the server.

Categories

Resources