customise contacts fields

Ayoub Ag. asked on May 12, 2015 15:36

Hi, i have a kentico instance behind a load balancer which prevent kentico's on-line marketing to get the right user's ip address,implies the geolocation via IP service returns always null position. i want to know if there is a way to overwrite the value of the IP address related to the contact?

regards,

Correct Answer

Brenden Kehren answered on May 12, 2015 17:30

Which version are you using? v6, v7, v8?

You have to ensure your load balancer is passing one of the parameters below through:

client-ip, x-forwarded-for, http_client_ip, http_x_forwarded_for

If it isn't passing one of those, none of the below will work.

For v6/7 I use this:

private void SetClientAddress(object sender, EventArgs e)
{
    string sClientAddress = null;
    NameValueCollection oColl = null;
    if ((HttpContext.Current != null) && (HttpContext.Current.Request != null))
    {
        oColl = HttpContext.Current.Request.Headers;
        foreach (string sKey in new string[] { "CLIENT-IP", "X-FORWARDED-FOR" })
        {
            sClientAddress = oColl[sKey];
            if ((sClientAddress != null) && (sClientAddress == "::1"))
            {
                sClientAddress = null;
            }
            else if (!String.IsNullOrWhiteSpace(sClientAddress))
            {
                break;
            }
        }
        if (String.IsNullOrWhiteSpace(sClientAddress))
        {
            oColl = HttpContext.Current.Request.ServerVariables;
            foreach (string sKey in new string[] { "HTTP_CLIENT_IP", "HTTP_X_FORWARDED_FOR" })
            {
                sClientAddress = oColl[sKey];
                if ((sClientAddress != null) && (sClientAddress == "::1"))
                {
                    sClientAddress = null;
                }
                else if (!String.IsNullOrWhiteSpace(sClientAddress))
                {
                    break;
                }
            }
        }
    }
    if (!String.IsNullOrWhiteSpace(sClientAddress))
    {
        RequestStockHelper.Add("UserHostAddress", sClientAddress);
    }
}

In v8, you have to change

RequestStockHelper.Add("UserHoseAddress", sClientAddress);

To:

RequestContext.UserHostAddress = sClientAddress;
3 votesVote for this answer Unmark Correct answer

Recent Answers


Ayoub Ag. answered on May 12, 2015 17:33

sorry i'm using v7

0 votesVote for this answer Mark as a Correct answer

Ayoub Ag. answered on May 12, 2015 19:31

thank you Brenden Kehren

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.