I came across a scenario where my client wanted to personalize the content based on country of visitor.
Sitecore has its own IP Geolocation Service which provides information about the location and owner of an IP address, beyond that provided by a reverse DNS lookup. IP Geolocation information includes the country, state, city, and the registered company name of every visitor.
But it’s a paid service and my client wanted to explorer if we can use any free version for getting the country.
I already knew about MAXMIND which operates both ways – Paid as well as free version.
There are few limitations with the free version:
- There are no automatic updates for the database, but you can write your own script to do so.
- Free database updates are available a week or so later then the paid once.
Our client agreed upon the limitations and we started the development.
- We created an account on maxmind – https://www.maxmind.com/en/geolite2/signup
- Once the account is up and running, you can go to https://www.maxmind.com/en/accounts/362463/geoip/downloads and download the database as per your need.

After the download is complete, unzip the files and copy “GeoLite2-Country.mmdb” file into your project.
Below is code sample to get the country from maxmind database from visitor IP address:
public class GetLocation
{
private ILog log = LogManager.GetLogger("Geo-IP");
private static GetLocation _user;
public static System.Net.IPAddress ip;
public static GetLocation User => _user ?? (_user = new GetLocation());
private static IPAddress GetIpAddressFromTracker()
{
//True-Client-IP -> if you have AKAMAI in-place.
string remoteAddr = HttpContext.Current.Request.Headers["True-Client-IP"];
if (string.IsNullOrEmpty(remoteAddr))
{
string forwardedFor = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (!string.IsNullOrEmpty(forwardedFor))
{ remoteAddr = forwardedFor.Split(',').Select(s => s.Trim()).First(); }
else
{ return new IPAddress(Tracker.Current.Interaction.Ip); }
}
try
{
IPAddress Address;
if (!string.IsNullOrEmpty(remoteAddr) && IPAddress.TryParse(remoteAddr, out Address))
{ return Address; }
return null;
}
catch
{ return new IPAddress(Tracker.Current.Interaction.Ip); }
}
private static string GetCountryFromMaxMind(IPAddress ipAddress)
{
var db = new DatabaseReader(HttpContext.Current.Server.MapPath("/App_data/GeoLite2-Country.mmdb"), FileAccessMode.Memory);
MaxMind.GeoIP2.Responses.CountryResponse response;
return db.TryCountry(ipAddress, out response) ? response.Country.Name : string.Empty;
}
private static string GetCountryCode(string country)
{
var regions = CultureInfo.GetCultures(CultureTypes.SpecificCultures).Select(x => new RegionInfo(x.LCID));
return (regions.FirstOrDefault(region => region.EnglishName.ToLower().Contains((country.ToLower())))).TwoLetterISORegionName;
}
public Ip GetIpSettings()
{
var ipsettings = new Ip();
IPAddress ipAddress;
ipAddress = GetIpAddressFromTracker();
ipsettings.IpAddress = Lookup.Address.Tracker;
if (ipAddress != null && string.IsNullOrEmpty(ipsettings.Country))
{
ipsettings.LookupService = Lookup.Service.MaxMindGeoIp2;
ipsettings.Country = GetCountryFromMaxMind(ipAddress);
}
if (ipAddress != null && !string.IsNullOrEmpty(ipsettings.Country))
{
ipsettings.CountryCode = GetCountryCode(ipsettings.Country).ToUpper();
}
return ipsettings;
}
}
public class Ip
{
public string CountryCode { get; set; }
public string Country { get; set; }
public Lookup.Address IpAddress { get; set; }
public Lookup.Service LookupService { get; set; }
}
public class Lookup
{
public enum Service
{
SitecoreGeoIp,MaxMindGeoIp2
}
public enum Address
{
Tracker,QueryString,Empty
}
}
Once this is done, you can use above functions to get the country name or country code as per your need, for e.g:
var countryobj = GetLocation.User.GetIpSettings();
string country_code = countryobj.CountryCode;
There are couple of ways provided by MAXMIND for retrieving the geolocation:
If you find yourself in a similar situation and need to use MAXMIND for getting the information, now you know how to make it work.
Until next time, cheers
{:}{:ja}サイト訪問者の国に従ってコンテンツをパーソナライズしたい場合どうするかと?
Sitecoreが提供されているIPジオロケーションサービスがあり、DNS逆引き参照によって提供される情報以外に、IPアドレスの場所と所有者に関する情報を提供します。 IPジオロケーション情報には、国、州、都市、および各訪問者の登録会社名が含まれます。
しかし、それは有料サービスであり、国を取得するために無料バージョンを使用できるかどうかを調査する必要があります。
それについてですがMAXMINDが同じようなサービスを提供されていることがわかっていて、MAXMINDの場合有料版と無料版両方も提供しています。
でも無料版にはいくつかの制限があります。
- データベースの自動更新はありませんが、独自のスクリプトを作成して自動更新を実装することができます。
- しかし有料版の更新後一週間に無料版のためにも更新情報が出てきます。
上記の制限で無料版を利用したい場合以下の手順になります。
- MAXMINDでアカウントを作ってください、以下のURLでできます。https://www.maxmind.com/en/geolite2/signup
- アカウントが稼働したら、
アカウントを作成後に以下のURLから要件に従てデータベースをダウンロードすること
https://www.maxmind.com/en/accounts/362463/geoip/downloads

ダウンロード後ファイルを展開し、「GeoLite2-Country.mmdb」ファイルをプロジェクトにコピーしてください。
訪問者のIPアドレスを利用してMAXMINDのデータベースから国を取得したい場合以下のコードを参照にしてください。
public class GetLocation
{
private ILog log = LogManager.GetLogger("Geo-IP");
private static GetLocation _user;
public static System.Net.IPAddress ip;
public static GetLocation User => _user ?? (_user = new GetLocation());
private static IPAddress GetIpAddressFromTracker()
{
//True-Client-IP -> if you have AKAMAI in-place.
string remoteAddr = HttpContext.Current.Request.Headers["True-Client-IP"];
if (string.IsNullOrEmpty(remoteAddr))
{
string forwardedFor = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (!string.IsNullOrEmpty(forwardedFor))
{ remoteAddr = forwardedFor.Split(',').Select(s => s.Trim()).First(); }
else
{ return new IPAddress(Tracker.Current.Interaction.Ip); }
}
try
{
IPAddress Address;
if (!string.IsNullOrEmpty(remoteAddr) && IPAddress.TryParse(remoteAddr, out Address))
{ return Address; }
return null;
}
catch
{ return new IPAddress(Tracker.Current.Interaction.Ip); }
}
private static string GetCountryFromMaxMind(IPAddress ipAddress)
{
var db = new DatabaseReader(HttpContext.Current.Server.MapPath("/App_data/GeoLite2-Country.mmdb"), FileAccessMode.Memory);
MaxMind.GeoIP2.Responses.CountryResponse response;
return db.TryCountry(ipAddress, out response) ? response.Country.Name : string.Empty;
}
private static string GetCountryCode(string country)
{
var regions = CultureInfo.GetCultures(CultureTypes.SpecificCultures).Select(x => new RegionInfo(x.LCID));
return (regions.FirstOrDefault(region => region.EnglishName.ToLower().Contains((country.ToLower())))).TwoLetterISORegionName;
}
public Ip GetIpSettings()
{
var ipsettings = new Ip();
IPAddress ipAddress;
ipAddress = GetIpAddressFromTracker();
ipsettings.IpAddress = Lookup.Address.Tracker;
if (ipAddress != null && string.IsNullOrEmpty(ipsettings.Country))
{
ipsettings.LookupService = Lookup.Service.MaxMindGeoIp2;
ipsettings.Country = GetCountryFromMaxMind(ipAddress);
}
if (ipAddress != null && !string.IsNullOrEmpty(ipsettings.Country))
{
ipsettings.CountryCode = GetCountryCode(ipsettings.Country).ToUpper();
}
return ipsettings;
}
}
public class Ip
{
public string CountryCode { get; set; }
public string Country { get; set; }
public Lookup.Address IpAddress { get; set; }
public Lookup.Service LookupService { get; set; }
}
public class Lookup
{
public enum Service
{
SitecoreGeoIp,MaxMindGeoIp2
}
public enum Address
{
Tracker,QueryString,Empty
}
}
これが完了したら、下記の関数を使用して、必要に応じて国名または国コードを取得できます。
例:
var countryobj = GetLocation.User.GetIpSettings();
string country_code = countryobj.CountryCode;
ジオロケーションを取得するためにMAXMINDが提供されている方法はいくつかあります。
- https://maxmind.github.io/GeoIP2-dotnet/
- 同じためにmaxmindNuGetAPIを使用することもできます。
同様の状況にあり、情報を取得するためにMAXMINDを使用する必要がある場合は、それを機能させる方法がわかりました。
次回まで、乾杯
{:}
Leave a Reply