Location Personalization with IP address

Posted by Community Admin on 03-Aug-2018 19:54

Location Personalization with IP address

All Replies

Posted by Community Admin on 03-Dec-2013 00:00

I have problem using location personalization. I understand that location personalization is based on IP address of the incoming request and sitefinity tries to match the IP address against a location+ IP database.

Is the ip address sitefinity uses for location personalzation the same as the ip address in the following function? based on servervariables  ["HTTP_X_FORWARDED_FOR"] or ["REMOTE_ADDR"]? I appreciate if someone can answer because it helps to debug the issue. 

public
static String GetIP()


String ip =

        HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
  
if (string.IsNullOrEmpty(ip))



ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];

   


 

   
return ip;



www.sitefinity.com/.../access-from-a-certain-ip-address




Posted by Community Admin on 04-Dec-2013 00:00

Hello David,

We get the IP address from the current request context (UserHostAddress property), see below:

protected virtual IPAddress VisitorIP
    get
    
        if (string.IsNullOrEmpty(this.Context.Request.UserHostAddress))
            return IPAddress.None;
 
        IPAddress ip = IPAddress.None;
        if (IPAddress.TryParse(this.Context.Request.UserHostAddress, out ip))
        
            return ip;
        
        return IPAddress.None;
    


Regards,
Radoslav Georgiev
Telerik
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

Posted by Community Admin on 04-Dec-2013 00:00

Thanks Rado. so according to your answer, if a load balancer proxy server is used, for location personalization purpose, sitefinity will use "this.Context.Request.UserHostAddress", which is the IP address of the proxy server?
If it is the case, from what I have read, the originating IP address is  in the HTTP_X_FORWARDED_FOR variable. Is there a way to override the way sitefinity works get the ip address(VisitorIP method) so we can use the HTTP_X_FORWARDED_FOR instead for location personalziation purpose?

Posted by Community Admin on 06-Dec-2013 00:00

Hello David,

You can substitute the built in evaluators for IP and Location in order to get the user IP in the way you suggest. This can be used as a solution while I get to the development team and make changes in the built in code. So for example create 2 classes which will override the built in evaluators:
Location:

using Telerik.Sitefinity.Personalization.Impl.Evaluators;
 
namespace SitefinityWebApp
    public class CustomLocationEvaluator : LocationEvaluator
    
        protected override System.Net.IPAddress VisitorIP
        
            get
            
                //replace default implementation with yours
                return base.VisitorIP;
            
        
    

IP:
using Telerik.Sitefinity.Personalization.Impl.Evaluators;
 
namespace SitefinityWebApp
    public class CustomIPAddressEvaluator : IPAddressEvaluator
    
        protected override System.Net.IPAddress VisitorIP
        
            get
            
                //replace default implementation with yours
                return base.VisitorIP;
            
        
    

Then what you will need to do is to register the custom classes with the Unity container and replace the default implementation. This can be done in Global.asax:
protected void Application_Start(object sender, EventArgs e)
    Bootstrapper.Initialized += Bootstrapper_Initialized;
 
void Bootstrapper_Initialized(object sender, Telerik.Sitefinity.Data.ExecutedEventArgs e)
    if (e.CommandName == "Bootstrapped")
    
        //replace the built in Location evaluator with custom one
        ObjectFactory.Container.RegisterType(
                typeof(ICriterionEvaluator),
                typeof(CustomLocationEvaluator),
                PersonalizationConstants.CriteriaName.Location,
                new ContainerControlledLifetimeManager(),
                new InjectionConstructor());
        //replace the built in IP evaluator with custom one
        ObjectFactory.Container.RegisterType(
            typeof(ICriterionEvaluator),
            typeof(CustomIPAddressEvaluator),
            PersonalizationConstants.CriteriaName.IPAddress,
            new ContainerControlledLifetimeManager(),
            new InjectionConstructor());
    


Regards,
Radoslav Georgiev
Telerik
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

This thread is closed