【问题标题】:Get Client Ip Kentico 9获取客户端 Ip Kentico 9
【发布时间】:2018-04-20 19:26:36
【问题描述】:

我需要获取访问该站点的客户所在的国家/地区。 我可以使用GeoIPHelper.GetCountryByIp("ip")获取国家/地区,但是如何获取访问该站点的客户端的IP地址?

【问题讨论】:

  • 您是否在网络环境中使用负载均衡器?

标签: ip kentico ip-geolocation


【解决方案1】:

您使用的是 Kentico EMS 吗?如果是,则获取当前联系人所在的国家/地区:

var countryId = ContactManagementContext.CurrentContact.ContactCountryId;
var country = CoutryInfoProvider.GetCountryInfo(countryId);

或者

var currentLocation = GeoIPHelper.GetCurrentGeoLocation();

其中还包含基于当前请求的国家/地区。

【讨论】:

    【解决方案2】:

    {%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>
    

    【讨论】:

      【解决方案3】:

      查看您的代码示例,我认为您在某处的代码中。对于 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 方法。

      【讨论】:

        【解决方案4】:
        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%}
        

        【讨论】:

          猜你喜欢
          • 2020-12-08
          • 1970-01-01
          • 1970-01-01
          • 2012-09-25
          • 2013-10-16
          • 2014-10-06
          • 2019-01-14
          • 2012-03-31
          • 2011-02-24
          相关资源
          最近更新 更多