World is now on Opti ID! Learn more

sanjay.kumar
May 4, 2021
  45
(0 votes)

Update GeoIP2 database automatically

The purpose of the blog to update the latest GeoIP2 database automatically. Because the GeoLite2 Country, City, and ASN databases are updated weekly, every Tuesday of the week. So we required to update the database up to date, which we can accomplish through the schedule job.

About GeoIP2:

MaxMind GeoIP2 offerings provide IP geolocation and proxy detection for a wide range of applications including content customization, advertising, digital rights management, compliance, fraud detection, and security.

The GeoIP2 Database MaxMind provides both binary and CSV databases for GeoIP2. Both formats provide additional data not available in our legacy databases including localized names for cities, subdivisions, and countries.

 

Requirement:

In one of my current projects, we required to read the zip code/postal code on basis of the client IP Address and populate into the address area. Similarly, you can retrieve the Country, City Name, Latitude & Longitude, Metro Code etc.

Solution:

  1. Create the schedule job and download GeoLiteCity.dat.gz.
  2. Uncompressed the file and copy the GeoLite2-City.mmdb database file on physical location which you have define in basePath.
  3. Read the client IP Address.
  4. Read the downloaded GeoLite2-City.mmdb database through DatabaseReader and search the IP address into city database and retrieve zip code/postal code.

 

Okay, so let's do some coding for achieving the above approaches:

Step1: Create the schedule job.

public class GeoIp2DataBaseUpdateJob : ScheduledJobBase
{
        private bool _stopSignaled;
        private const string GeoLiteCityFileDownloadUrl = "https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-City&suffix=tar.gz";

        public GeoIp2DataBaseUpdateJob()
        {
            this.IsStoppable = true;
        }

        /// <summary>
        /// Called when a user clicks on Stop for a manually started job, or when ASP.NET shuts down.
        /// </summary>
        public override void Stop()
        {
            base.Stop();
            _stopSignaled = true;
        }

        /// <summary>
        /// Called when a scheduled job executes
        /// </summary>
        /// <returns>A status message to be stored in the database log and visible from admin mode</returns>
        public override string Execute()
        {
                   // 1. Download file
            var result = DownloadFile()
                // 2. Unzip file
                .Bind(UnzipFile)
                // 3. Replace at physical location
                .Bind(ReplaceCurrentFile)
                .Match(
                    right => $"👍 GeoIP2 database updated successfully.",
                    left => $"👎 GeoIP2 database update failed.");

            if (_stopSignaled)
            {
                return "Stop of job was called";
            }

            return result;
        }

        private static Either<Exception, string> DownloadFile()
        {
            var licenseKey = ConfigurationManager.AppSettings["Geolocation.LicenseKey"];
            var uri = new Uri(GeoLiteCityFileDownloadUrl + $"&license_key={licenseKey}");

            return Prelude.Try(
                    () =>
                    {
                        using (var client = new WebClient())
                        {
                            var tempDir = Path.GetDirectoryName(Path.GetTempPath());
                            var localFile = Path.Combine(tempDir, "MaxMind/GeoLite2-City.tar.gz");
                            var maxMindFolderPath = Path.GetDirectoryName(localFile);
                            if (!Directory.Exists(maxMindFolderPath) && !string.IsNullOrWhiteSpace(maxMindFolderPath))
                            {
                                Directory.CreateDirectory(maxMindFolderPath);
                            }

                            client.DownloadFile(uri, localFile);
                            return localFile;
                        }
                    })
                .Succ(localFile => Prelude.Right<Exception, string>(localFile))
                .Fail(ex => Prelude.Left<Exception, string>(ex));
        }

        private static Either<Exception, string> ReplaceCurrentFile(string unzippedFileName)
        {
            return Prelude.Try(
                    () =>
                    {
                        var maxMindDbFilePath = Path.Combine(EPiServerFrameworkSection.Instance.AppData.BasePath, "MaxMind/GeoLite2-City.mmdb");
                        File.Copy(unzippedFileName, maxMindDbFilePath, true);

                        //Delete extracted folder
                        var dir = Path.GetDirectoryName(unzippedFileName);
                        if (Directory.Exists(dir))
                        {
                            Directory.Delete(dir, true);
                        }
                        return unzippedFileName;

                    })
                .Succ(fileName => Prelude.Right<Exception, string>(fileName))
                .Fail(ex => Prelude.Left<Exception, string>(ex));
        }

        private static Either<Exception, string> UnzipFile(string downloadedFileName)
        {
            return Prelude.Try(
                    () =>
                    {
                        var dir = Path.GetDirectoryName(downloadedFileName);
                        FileInfo tarFileInfo = new FileInfo(downloadedFileName);

                        DirectoryInfo targetDirectory = new DirectoryInfo(dir ?? string.Empty);
                        if (!targetDirectory.Exists)
                        {
                            targetDirectory.Create();
                        }
                        using (Stream sourceStream = new GZipInputStream(tarFileInfo.OpenRead()))
                        {
                            using (TarArchive tarArchive = TarArchive.CreateInputTarArchive(sourceStream, TarBuffer.DefaultBlockFactor))
                            {
                                tarArchive.ExtractContents(targetDirectory.FullName);
                            }
                        }

                        var filePath = Directory.GetFiles(dir ?? string.Empty, "*.mmdb", SearchOption.AllDirectories)?.LastOrDefault();

                        //Delete .tar.gz file
                        if (File.Exists(downloadedFileName))
                        {
                            File.Delete(downloadedFileName);
                        }
                        return filePath;
                    })
                .Succ(fileName => Prelude.Right<Exception, string>(fileName))
                .Fail(ex => Prelude.Left<Exception, string>(ex));
        }

    }

Step 2: Update the web.config settings

  1. Set the database file path for geolocation provider if you are using for personalization.
  2. Add the basePath for updating the file on a given physical location.
  3. Set MaxMind license key
<episerver.framework>
  ...
  <geolocation defaultprovider="maxmind">
    <providers>
      <add databasefilename="C:\Program Files (x86)\MaxMind\GeoLite2-City.mmdb" name="maxmind" type="EPiServer.Personalization.Providers.MaxMind.GeolocationProvider, EPiServer.ApplicationModules">
    </add></providers>
  </geolocation>
  <appData basePath="C:\Program Files (x86)\MaxMind" />
</episerver.framework>

<appSettings>
 ...
 <add key="Geolocation.LicenseKey" value="{YourLicenseKey}" 
</appSettings>

Step 3: Run the job and make sure the file is updating, if the code cause the error then update accordingly.

Step4: Create the GeoLocationUtility class and read the database file for client IP Address.

 public class GeoLocationUtility
 {
        private static string _maxMindDatabaseFileName = "GeoLite2-City.mmdb";

        public static GeoLocationViewModel GetGeoLocation(IPAddress address, NameValueCollection config)
        {
            string text = config["databaseFileName"];

            if (!string.IsNullOrEmpty(text))
            {
                _maxMindDatabaseFileName = VirtualPathUtilityEx.RebasePhysicalPath(text);
                config.Remove("databaseFileName");
            }

            if (string.IsNullOrWhiteSpace(_maxMindDatabaseFileName)
                || !File.Exists(_maxMindDatabaseFileName)
                || address.AddressFamily != AddressFamily.InterNetwork && address.AddressFamily != AddressFamily.InterNetworkV6)
            {
                return null;
            }

            var reader = new DatabaseReader(_maxMindDatabaseFileName);
            try
            {
                var dbResult = reader.City(address);
                var result = GeoLocationViewModel.Make(dbResult);
                return result;
            }
            catch
            { 
                //ignore exception
            }
            finally
            {
                reader.Dispose();
            }

            return null;
        }

Step 5: Finally, call the utility method where you want to get the zip code/postal code value such as:

  var maxMindDbFilePath = Path.Combine(EPiServerFrameworkSection.Instance.AppData.BasePath, "MaxMind/GeoLite2-City.mmdb");
   var config = new NameValueCollection
   {
    {"databaseFileName", maxMindDbFilePath}
   };

   var  postalCode= GeoLocationUtility.GetGeoLocation(ipAddress, config)?.PostalCode;

I hope you found this informative and helpful, Please leave your valuable comments in the comment box.

Thanks for your visit!

May 04, 2021

Comments

Please login to comment.
Latest blogs
Make Global Assets Site- and Language-Aware at Indexing Time

I had a support case the other day with a question around search on global assets on a multisite. This is the result of that investigation. This co...

dada | Jun 26, 2025

The remote server returned an error: (400) Bad Request – when configuring Azure Storage for an older Optimizely CMS site

How to fix a strange issue that occurred when I moved editor-uploaded files for some old Optimizely CMS 11 solutions to Azure Storage.

Tomas Hensrud Gulla | Jun 26, 2025 |

Enable Opal AI for your Optimizely products

Learn how to enable Opal AI, and meet your infinite workforce.

Tomas Hensrud Gulla | Jun 25, 2025 |

Deploying to Optimizely Frontend Hosting: A Practical Guide

Optimizely Frontend Hosting is a cloud-based solution for deploying headless frontend applications - currently supporting only Next.js projects. It...

Szymon Uryga | Jun 25, 2025

World on Opti ID

We're excited to announce that world.optimizely.com is now integrated with Opti ID! What does this mean for you? New Users:  You can now log in wit...

Patrick Lam | Jun 22, 2025

Avoid Scandinavian Letters in File Names in Optimizely CMS

Discover how Scandinavian letters in file names can break media in Optimizely CMS—and learn a simple code fix to automatically sanitize uploads for...

Henning Sjørbotten | Jun 19, 2025 |