Client IP Address in Load Balanced Scenario

Brenden Kehren asked on January 12, 2015 21:42

Does anyone know if this article is still pertinent in v8.x? I've been able to implement it in v7 without issue but it doesn't seem to be working in v8.x at all.

Correct Answer

Brenden Kehren answered on March 6, 2015 14:08

Thanks for following up Radek. After finally having a bit of time to debug further I found a simple change was needed in the last few lines to assign the actual address.

Old code:

if (!String.IsNullOrWhiteSpace(sClientAddress))
{
    RequestStockHelper.Add("UserHostAddress", sClientAddress);
}

New code:

if (!String.IsNullOrWhiteSpace(sClientAddress))
{
    RequestContext.UserHostAddress = sClientAddress;
}
1 votesVote for this answer Unmark Correct answer

Recent Answers


Radek Macalik answered on March 6, 2015 12:38 (last edited on March 6, 2015 12:38)

Hello.

The article is written and tested for version 7 only. We did not test it for any newer version. Anyways, the problem might be related to differences in API and project structure between Kentico 7 and Kentico 8. Could you please send us an e-mail to support@kentico.com and refer to this forum thread? Thank you.

Best regards, Radek Macalik

0 votesVote for this answer Mark as a Correct answer

Pedro Costa answered on July 9, 2015 12:27

Hi,

We had to change one more line of code for 8.2 in the latest hotfix, instead of :

CMSRequestEvents.Begin.Before += new EventHandler<EventArgs>(SetClientAddress);

use:

RequestEvents.Begin.Execute += new EventHandler<EventArgs>(SetClientAddress);

Not validated by Kentico, but I think that's the correct new method to handle this event.

Cheers, P.

0 votesVote for this answer Mark as a Correct answer

Trevor Orr answered on February 8, 2017 00:50

For Kentico 10 using AWS ELB start with the code here for adding Global Events: https://docs.kentico.com/k10/custom-development/handling-global-events

And you can use this code:

using CMS;
using CMS.Base;
using CMS.DataEngine;
using CMS.Helpers;
using System;
using System.Web;

// Registers the custom module into the system
[assembly: RegisterModule(typeof(CustomInitializationModule))]

public class CustomInitializationModule : Module
{
    // Module class constructor, the system registers the module under the name "CustomInit"
    public CustomInitializationModule() : base("CustomInit")
    {
    }

    // Contains initialization code that is executed when the application starts
    protected override void OnInit()
    {
        base.OnInit();

        // Assigns custom handlers to events
        RequestEvents.Begin.Execute += new EventHandler<EventArgs>(SetClientAddress);
    }

    void SetClientAddress(object sender, EventArgs e)
    {
        if ((HttpContext.Current != null) && (HttpContext.Current.Request != null))
        {
            var headers = HttpContext.Current.Request.Headers;
            // AWS passes in the x-forwarded-for header... so look for that
            var forwardedFor = headers.Get("X-Forwarded-For");
            if (!String.IsNullOrWhiteSpace(forwardedFor) && (forwardedFor != "::1"))
            {
                RequestContext.UserHostAddress = forwardedFor;
            }
        }
    }
}
0 votesVote for this answer Mark as a Correct answer

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