1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
private string GetCustomerIP()
{
string CustomerIP = "";
try
{
if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_VIA"] != null)
{
CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
}
else
{
CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString();
}
}
catch
{ }
try
{
CustomerIP = System.Web.HttpContext.Current.Request.UserHostAddress;
}
catch
{ }
try
{
CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
}
catch
{ }
try
{
string clientIP;
string ip = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (!string.IsNullOrEmpty(ip))
{
string[] ipRange = ip.Trim().Split(',');
int le = ipRange.Length - 1;
clientIP = ipRange[le];
}
else
clientIP = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
}
catch
{ }
// Get message properties from current OperationContext
System.ServiceModel.Channels.MessageProperties props = System.ServiceModel.OperationContext.Current.IncomingMessageProperties;
// Find the RemoteEndpointMessageProperty
System.ServiceModel.Channels.RemoteEndpointMessageProperty prop = (System.ServiceModel.Channels.RemoteEndpointMessageProperty)props[System.ServiceModel.Channels.RemoteEndpointMessageProperty.Name];
// Retrieve the IP address
string addr = prop.Address;
int iPort = prop.Port;
return addr;
} |
Partager