Click here to Skip to main content
16,019,876 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Hi Friends,

I m developing an API using WCF restful webservices for travel domain.

We are getting API requests from client server and we are trying to validate the client based on the server's IP address. I want to get the IP address of client.

We are using the following code to extract the clients server IP:

C#
OperationContext context = OperationContext.Current;
MessageProperties messageProperties = context.IncomingMessageProperties;
RemoteEndpointMessageProperty endpointProperty = messageProperties[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;

string Ip_addr = endpointProperty.Address;


But we are not getting the exact IP of the client server. we are getting a single IP address for all the request.

For example,
I am getting a request from client AA or BB, I am getting the IP address as : 192.168.10.1


Any help!
Posted
Updated 5-Aug-13 2:47am
v2
Comments
Sergey Alexandrovich Kryukov 1-Aug-13 13:09pm    
What is "clients server IP"? :-)
—SA

I really think you should check out this CP post...

Get client IP address in a WCF Service hosted using HTTPS 443 bindings[^]
 
Share this answer
 
string ip = System.Web.HttpContext.Current.Request.UserHostAddress;
 
Share this answer
 
I assume that you want client IP:

C#
public static string GetUserIP()
{
    string ipList = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

    if (!string.IsNullOrEmpty(ipList))
    {
        return ipList.Split(',')[0];
    }

    if (!string.IsNullOrWhiteSpace(HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]))
    {
        return HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
    }

    return HttpContext.Current.Request.UserHostAddress;
}
 
Share this answer
 
C#
string strHostName = "";
            strHostName = System.Net.Dns.GetHostName();

            System.Net.IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(strHostName);

            System.Net.IPAddress[] addr = ipEntry.AddressList;

            label1.Text = addr[addr.Length - 1].ToString();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900