Contact geolocation doesn't work behind Azure CDN

Dmitry Bastron asked on March 30, 2020 17:44

Hi guys! We have Kentico 12 SP MVC website hosted as Azure Web App behind Azure CDN. CDN is configured to cache only images (HTML is not cached). We have the following problem: contact geolocation via MaxMind doesn't work. Instead, every time the system detects location of CDN servers rather than actual contact. Is there any way to configure CDN to pass additional header or something that will solve this problem?

Correct Answer

Brian McKeiver answered on March 30, 2020 18:21

We've done stuff like this in the past Dmitry:

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 handler to begin request event
        RequestEvents.Begin.Execute += new EventHandler<EventArgs>(GetRealClientIpAddress);
    }
    void GetRealClientIpAddress(object sender, EventArgs e)
    {
        if ((HttpContext.Current != null) && (HttpContext.Current.Request != null))
        {
            var headers = HttpContext.Current.Request.Headers;
            // look for the special header variable that the firewall or load balancer passes
            var realIP = headers.Get("SPECIAL HEADER VALUE");  //Make sure to change this to real value
            if (!String.IsNullOrWhiteSpace(realIP) && (realIP != "::1"))
            {
                RequestContext.UserHostAddress = realIP;
            }
        }
    }
}
2 votesVote for this answer Unmark Correct answer

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