GEO IP from code

Vote:
 

Hi


Im trying to filter pages from code behind.

I have visitor groups created on different areas of sweden.

When a user arrives to the page I collect a list of pages, customer service.

I want to do a check from code which page to present data data from but also be able to override the visitor group.

If i am in Stockholm i should get the customer service from stockholm but also be able to change and view Gothenburg.

I cant seem to find any examples of how to do this from code behind.

 

/Pär

#79286
Dec 12, 2013 13:53
Vote:
 

Hi, 

 

this is what I've done in a project: 

 public IPAddress GetIPAddress()
        {
            var request = System.Web.HttpContext.Current.Request;

            string ipString;
            if (String.IsNullOrEmpty(request.ServerVariables["HTTP_X_FORWARDED_FOR"]))
            {
                ipString = request.ServerVariables["REMOTE_ADDR"];
            }
            else
            {
                ipString = request.ServerVariables["HTTP_X_FORWARDED_FOR"].Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)
                    .FirstOrDefault();
            }

            IPAddress result;
            if (!IPAddress.TryParse(ipString, out result))
            {
                result = IPAddress.None;
            }

            return result;
        }

    

Then, later by using episervers geolocation (maxmind) api, 

var prov = Geolocation.Provider;
var result = prov.Lookup(ipAddress);

 

Hope it works.

 

 

#79575
Dec 20, 2013 10:06
Vote:
 

I got a point in the right direction and the solution was pretty simple


public static List<VisitorGroup> GetUserVisitorGroups()
        {
            var visitorGroupHelper = new VisitorGroupHelper();
            var visitorGroups = new VisitorGroupStore();
            IEnumerable<VisitorGroup> groups = visitorGroups.List();

            var userVisitorGroups = new List<VisitorGroup>();

            foreach (var group in groups)
            {
                if (visitorGroupHelper.IsPrincipalInGroup(Thread.CurrentPrincipal, group.Name, new HttpContextWrapper(HttpContext.Current)))
                {
                    userVisitorGroups.Add(group);
                }
            }
            return userVisitorGroups;
        }


I then compared the groups with the  ACL key on the page(i set the visitor group as authorized to view the page)

#79672
Edited, Dec 30, 2013 1:45
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* 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.