需要客户端服务器的IP地址 [英] Need IP address of client server

查看:144
本文介绍了需要客户端服务器的IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友们,



我正在使用WCF restful webservices为旅行域开发API。



我们正在从客户端服务器获取API请求,我们正在尝试根据服务器的IP地址验证客户端。我想获取客户端的IP地址。



我们使用以下代码提取客户端服务器IP:



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:

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

string Ip_addr = endpointProperty.Address;





但我们没有得到客户端服务器的确切IP。我们为所有请求获取一个IP地址。



例如,

我收到客户AA或BB的请求,我得到的IP地址为:192.168.10.1





任何帮助!



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!

推荐答案

我真的认为你应该查看这篇CP帖子......



在使用HTTPS 443绑定托管的WCF服务中获取客户端IP地址 [ ^ ]
I really think you should check out this CP post...

Get client IP address in a WCF Service hosted using HTTPS 443 bindings[^]


string ip = System.Web.HttpContext.Current.Request.UserHostAddress;
string ip = System.Web.HttpContext.Current.Request.UserHostAddress;


我假设你想要客户端IP:



I assume that you want client IP:

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;
}


这篇关于需要客户端服务器的IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆