Location Personalization with IP address
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
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;
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?
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;
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;
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());