+1-888-365-2779
Try Now
More in this section

Forums / General Discussions / Location Personalization with IP address

Location Personalization with IP address

4 posts, 0 answered
  1. David
    David avatar
    4 posts
    Registered:
    27 Nov 2013
    03 Dec 2013
    Link to this post
    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;

    }

    http://www.sitefinity.com/developer-network/forums/security/access-from-a-certain-ip-address




  2. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    04 Dec 2013
    Link to this post
    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
  3. David
    David avatar
    4 posts
    Registered:
    27 Nov 2013
    04 Dec 2013 in reply to Radoslav Georgiev
    Link to this post
    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?
  4. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    06 Dec 2013
    Link to this post
    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
4 posts, 0 answered