Thursday, March 31, 2022

GeoIP

 Did not work for me per documentation. But used test utility and used that code



using Lincare.Feature.LocationSearch.Models;

using Sitecore.Data;

using Sitecore.Data.Items;

using Sitecore.Diagnostics;

using Sitecore.XA.Foundation.Abstractions;

using Sitecore.XA.Foundation.Mvc.Controllers;

using System;

using System.Net;

using System.Web.Mvc;

using Lincare.Feature.LocationSearch.Cache;

using Lincare.Feature.LocationSearch.Services;

using Sitecore.Analytics.Lookups;

using Sitecore.Analytics.Model;

using Sitecore.DependencyInjection;

using Microsoft.Extensions.DependencyInjection;

using Sitecore.CES.GeoIp.Core;

using Sitecore.CES.GeoIp.Core.Model;

using Sitecore.Analytics;

using Sitecore.CES.GeoIp.Core.Lookups;

var latitude = Sitecore.Analytics.Tracker.Current.Interaction.GeoData.Latitude;

var longitude = Sitecore.Analytics.Tracker.Current.Interaction.GeoData.Longitude;





String ip = Request.ServerVariables["REMOTE_ADDR"];


if (string.IsNullOrEmpty(ip))

{

ip = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

}


TimeSpan millisecondsTimeout2 = new TimeSpan(0, 0, 0, 0, 2000);


var geoIpManager = ServiceLocator.ServiceProvider.GetRequiredService<IGeoIpManager>();

var geoIpFetchedData = geoIpManager.GetGeoIpData(ip, millisecondsTimeout2);


if (geoIpFetchedData.Status == GeoIpFetchDataStatus.Fetched && geoIpFetchedData.WhoIsInformation != null)

{

country = geoIpFetchedData.WhoIsInformation.Country;

}



//////////////////// with deprecated method


IPAddress address = IPAddress.Parse(ip);


var millisecondsTimeout = 2000;

var options = new GeoIpOptions(address, millisecondsTimeout);

//var options = new Sitecore.CES.GeoIp.Core.Resolver.Managers.GeoIpResolverManagerBase(address, millisecondsTimeout);


var result = GeoIpManager.GetGeoIpData(options);

if (result.ResolveState == GeoIpResolveState.Resolved &&

result.WhoIsInformation != null)

{

country = result.WhoIsInformation.Country;

}




But this worked:

https://support.sitecore.com/kb?id=kb_article_view&sysparm_article=KB0798498


Sitecore 10

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TestIp.aspx.cs" %>

<%@ Import Namespace="Sitecore.CES.GeoIp.Core.Lookups" %>


<!DOCTYPE html>


<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title></title>

</head>

    <script runat="server">

        protected void Button1_Click(object sender, EventArgs e)

        {

            var w = LookupManager.GetWhoIsInformationByIp(TextBox1.Text);

                  }

    </script>

///////////////////////////////

Sitecore 9

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TestIp.aspx.cs" %>

<%@ Import Namespace="Sitecore.Analytics.Lookups" %>


<!DOCTYPE html>


<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title></title>

</head>

    <script runat="server">

        protected void Button1_Click(object sender, EventArgs e)

        {

            var w = LookupManager.GetInformationByIp(TextBox1.Text);

No comments:

Post a Comment