我需要获得访问该网站的客户所在的国家/地区。
我可以使用GeoIPHelper.GetCountryByIp("ip")
来获取Counry,但是如何获取访问该网站的客户端的IP地址?
答案 0 :(得分:1)
您使用的是Kentico EMS吗?如果是,那么获得当前联系的国家:
var countryId = ContactManagementContext.CurrentContact.ContactCountryId;
var country = CoutryInfoProvider.GetCountryInfo(countryId);
或者
var currentLocation = GeoIPHelper.GetCurrentGeoLocation();
其中还包含基于当前请求的国家/州。
答案 1 :(得分:0)
{%Ip%}
或var clientIp = CMS.Helpers.RequestContext.UserHostAddress
或者您想找出现有客户的IP吗?
这将是一个像
这样的SQL查询select Convert(xml,UserLastLogonInfo)
from COM_Customer Join CMS_User on CustomerUserID = UserID
它会给你的xml像:
<info>
<agent></agent>
<ip></ip>
</info>
答案 2 :(得分:0)
查看您的代码示例,我认为您在某处的代码中。这可能是ASP.NET的一般性问题。如果是这种情况,您可以使用HttpContext.Current.Request.UserHostAddress
获取当前用户的IP。
如果您背后有类似负载均衡器的东西,可能会出现问题。为了解决这个问题,您可以尝试以下方法:
string userAddress = string.Empty;
if (HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null)
{
userAddress = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
}
else if (HttpContext.Current.Request.UserHostAddress.Length != 0)
{
userAddress = HttpContext.Current.Request.UserHostAddress;
}
var country = GeoIPHelper.GetCountryByIp(userAddress);
CMS.Helpers.RequestContext
也有效(正如Shof所说)但同样,请确保满足任何负载均衡器问题。说实话,我总是退回到只使用前Kentico习惯的HttpContext方法。
答案 3 :(得分:0)
GeoLocation currentGeoLocation = GeoIPHelper.GetCurrentGeoLocation();
if (currentGeoLocation != null)
{
var countryAndState = ValidationHelper.GetString(currentGeoLocation.CountryName, "");
var ipCountryCode = ValidationHelper.GetString(currentGeoLocation.CountryCode, "");
var ipRegion = ValidationHelper.GetString(currentGeoLocation.RegionName, "");
var ipCity = ValidationHelper.GetString(currentGeoLocation.City, "");
}
或宏
{% AnalyticsContext.CurrentGeoLocation.Country.CountryName%}
{% AnalyticsContext.CurrentGeoLocation.State%}
{% AnalyticsContext.CurrentGeoLocation.City%}
{% AnalyticsContext.CurrentGeoLocation.Postalcode%}