Issue w/ GeoLocation

A
A
Vote:
 

Ive created a new Criterion Class and am looking to restrict content by region. 

This is the criterion class

[VisitorGroupCriterion(DisplayName = "Region Only!")]
    public class RegionCriterion : CriterionBase
    {

        public override bool IsMatch(IPrincipal principal, HttpContextBase httpContext)
        {
            string usersIP = null;
            if (HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null)
            {
                usersIP = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
            }
            else
            {
                usersIP = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
            }
            var geoLocationProvider = Geolocation.Provider as GeolocationProvider;
            IPAddress address = IPAddress.Parse("usersIP");
            IGeolocationResult result = geoLocationProvider.Lookup(address);
            return (result.Region.Equals("myRegion")); ;
        }
    }

2 issues here.

First} Due to my setup my ip always comes back as ::1. Is there a way i can get around this for testing without hardcoding the ip address?

Second} result always returns null w/o error and I cant figure out why.

Ive followd the directions from here but still dont have a working set up ( I have the latest db and am able to run the update command successfully ):
https://world.episerver.com/documentation/developer-guides/CMS/personalization/Configuring-personalization/


Any thoughts?

CC

#192577
Edited, May 18, 2018 22:52
Vote:
 

Hi CC

This may help your question on 1. 

https://www.david-tec.com/2011/06/Using-the-IIS-rewrite-module-to-test-EPiServer-geo-IP-look-up-personalisation/

This may also help if you want to use an alternative header for IP addresses:

https://www.david-tec.com/2011/07/Ensure-EPiServer-Geo-IP-personalisation-works-when-using-Akamai/

David

#192590
May 20, 2018 8:58
Vote:
 

Hi CC,

The GeolocationProvider returns null in your case because the address is IPv6 address and it only supports IPv4 addresses.

If you enable debug logging and log messages from EPiServer.Personalization.Providers.MaxMind -namespace you can see in the logs message about IPv4 only supported.

#192591
May 20, 2018 17:53
A
Vote:
 

Thanks guys - both suggestions helped but now I'm confused about what is generating the path to the GeoLiteCity.dat file.

My GeoLiteCity.dat file sits here: ~/App_Data/GeoLiteCity.dat but whenever this runs:

Geolocation.Provider.Lookup(ipAddress)

It errors saying cant find file ~/GeoLiteCity.dat <--- missing App_Data/


What is creating the path to this file?

-----------------------------------
Update: adding web.xml config

<geolocation defaultProvider="maxmind">
      <providers>
        <add name="maxmind" type="EPiServer.Personalization.Providers.MaxMind.GeolocationProvider, EPiServer.ApplicationModules" databaseFileName="App_Data\GeoLiteCity.dat" />
      </providers>
</geolocation>



#192630
Edited, May 21, 2018 21:30
Vote:
 

Hi A,

Sorry been busy with work so I was not able to answer to you sooner.

You are using deprecated way of getting the GeolocationProvider (you haven't mentioned which version of Episerver packets you are using) but I did a quick test with Alloy using version 11.5.0 (EPiServer.CMS package).

The GeolocationProvider is registered to the IoC container so you should get it from there. You can use the normal ways of Episerver to get the service injected to your controllers for example (in the constructor like: EPiServer.Personalization.IGeolocationProvider geolocationProviderBase) but in your case you might use the Servicelocator like this: ServiceLocator.Current.GetInstance<IGeolocationProvider>();

So something like this should work for you:

// add usings
using EPiServer.ServiceLocation;
using EPiServer.Personalization;

// code
var provider = ServiceLocator.Current.GetInstance<IGeolocationProvider>();

// naturally replace the IP parsing with your code
IGeolocationResult result = provider.Lookup(System.Net.IPAddress.Parse("xxx.xxx.xxx.xxx"));

// and then use the result

Or you could use the Injected property in your criteria if you prefer that over ServiceLocator, https://world.episerver.com/documentation/Class-library/?documentId=cms/11/1692DF76

(as a side note your config values are defaults and look ok)

#193150
Edited, May 24, 2018 6:09
A
Vote:
 

Hi Antti - thanks for the reply.

Unfortunately I'm still getting null back as my result.
Im using ServiceLocator to get the instance of IGeolocationProvider, but whenever I call the LookUp( ) action, it always retruns null.

var provider = ServiceLocator.Current.GetInstance<IGeolocationProvider>();

var result = provider.Lookup(IPAddress.Parse("xxx.xxx.xxx.xxx"));

When a breakpoint is added and I hover over provider I see:

{EPiServer.Personalization.Providers.MaxMind.GeolocationProvider}

and when expanded I see properties for "Capabilities" which as a value of 0 and "Name" which has a value of 'default'.


adding logs: 

ERROR EPiServer.Personalization.Providers.MaxMind.GeolocationProvider: Geolocation database file cannot be found: C:\Users\<user>\Source\Repos\<myproject>\<myprojcet>\GeoLiteCity.dat

ERROR EPiServer.Personalization.Providers.MaxMind.GeolocationProvider: Error occured while looking up xxx.xxx.xxx.xxx : Path cannot be null.


So, right off the bat it's still not looking in the right spot, it should be looking in <myproject>/App_Data/.
Seems like its not using the 'databaseFileName' property from web.config

Thanks again for the assistance,

A

#193403
Edited, May 29, 2018 23:01
Vote:
 

Hi A,

While the path is very similar to yours I'm thinking that it's more likely that the provider uses the default database path, which happens to be "GeoLiteCity.dat". If this is the case it looks like it's completely disregarding your configuration for some reason. You should be able to confirm this by either changing the path in the web.config to something completely different or even go as far as removing the geolocation provider completely. If this is the case, could it be something simple such as the <geolocation> element is defined outside the <episerver.framework> section or similar?

Henrik

#193409
May 30, 2018 4:23
A
Vote:
 

Thanks Henrik,

I commented out the whole <geolocation> tag and it gave me the same exact result as before.

When I try moving <geolocation> outside of <episerver.framework> the project refuses to build.

When I move the GeoLiteCity.dat file to the spot it is already looking the errors stop, though I get the same result ( GeoLocationProvider comes through, LookUp( ) returns null )

Assuming the GeoLocationProvider does pull its config from web.config - I would agree it seems to be ignoring this file/config.

I am thoroughly stumped.

- A


update:

IGeolocationResult result = ServiceLocator.Current.GetInstance<IGeolocationProvider>().Lookup(IPAddress.Parse("xxx.xxx.xxx.xxx"));

plugged that ^^ into Alloy and result came back as null there too - not sure whats going on here.

#193517
Edited, May 30, 2018 17:15
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.