Getting the Correct IP Address
Learn how to extract client IP addresses in Umbraco Engage by implementing a custom IP address extractor for specific server environments.
using System.Web;
using Umbraco.Engage.Business.Analytics.Collection.Extractors;
public string ExtractIpAddress(HttpContextBase context)
{
if (context?.Request?.ServerVariables["X-Forwarded-For"] is string ipAddresses)
{
var ipAddress = ipAddresses.Split(',')[0].Trim();
if (System.Net.IPAddress.TryParse(ipAddress, out _)) return ipAddress;
}
return context?.Request?.UserHostAddress;
}using Umbraco.Engage.Business.Analytics.Collection.Extractors;
using Umbraco.Core.Composing;
using Umbraco.Core;
[ComposeAfter(typeof(Umbraco.Engage.Business.Analytics.Collection.Extractors.AnalyticsExtractorsComposer))]
public class CustomIpExtractorUserComposer : IUserComposer
{
public void Compose(Composition composition)
{
composition.RegisterUnique<IHttpContextIpAddressExtractor, MyIpAddressExtractor>();
}
}Last updated
Was this helpful?