public void ValidateUserData(string ip) { var contact = Tracker.Current?.Contact; if (contact == null) { Log.Debug($"[Demandbase Debug][xdb] Contact is null, trying to initialize"); Tracker.Initialize(); contact = Tracker.Current?.Contact; } var data = contact.GetFacet <IXdbFacetDemandbaseData>("Demandbase Data"); if (contact == null || _notFoundIps.Contains(ip) || ValidateUser.IsLocalSubnet(ip)) { Log.Debug($"[Demandbase Debug][xdb] IP is local subnet {ip} skipping data retrieval."); if (!string.IsNullOrWhiteSpace(data.DemandBaseData)) { data.DemandBaseData = ""; } return; } if (string.IsNullOrWhiteSpace(data.DemandBaseData) || (HasProperty(data.DemandBaseData, "ip") && data.Ip != ip)) { using (HttpClient wc = new HttpClient()) { wc.Timeout = TimeSpan.FromMilliseconds(Timeout); try { Log.Debug($"[Demandbase Debug][xdb] Retrieving data for ip {ip}"); string str = wc.GetStringAsync($"{DemandbaseContext.RestApi}?query={ip}&key={DemandbaseContext.Key}").Result; data.DemandBaseData = str; data.Ip = ip; Log.Debug($"[Demandbase Debug][xdb] Retrieved data for ip {ip} \nJSON:\n\n{str}"); } catch (Exception ex) { if (ex.ToString().Contains("404")) { Log.Warn("[Demandbase Debug][xdb] Ip address was not found in demandbase " + ip, this); _notFoundIps.Add(ip); } else { Log.Warn("[Demandbase Debug][xdb] unable to get demandbase user data for ip " + ip, ex, this); } data.DemandBaseData = ""; data.Ip = ip; } } } }
public void ValidateUserData(string ip) { var session = HttpContext.Current.Session; if (session == null || _notFoundIps.Contains(ip)) { Log.Debug($"[Demandbase Debug][session] Session is null or ip not found in demandbase {ip}"); return; } string user = session["demandBaseUser"]?.ToString(); string storedIp = session["demandBaseUserIp"]?.ToString(); if (string.IsNullOrWhiteSpace(user) || storedIp != ip || ValidateUser.IsLocalSubnet(ip)) { using (HttpClient wc = new HttpClient()) { wc.Timeout = TimeSpan.FromMilliseconds(Timeout); try { Log.Debug($"[Demandbase Debug][session] Retrieving data for ip {ip}"); string str = wc.GetStringAsync($"{DemandbaseContext.RestApi}?query={ip}&key={DemandbaseContext.Key}").Result; session["demandBaseUser"] = str; session["demandBaseUserIp"] = ip; Log.Debug($"[Demandbase Debug][session] Retrieved data for ip {ip}\nJSON:\n\n{str}"); } catch (Exception ex) { if (ex.ToString().Contains("404")) { Log.Warn("[Demandbase Debug][session] Ip address was not found in demandbase " + ip, this); _notFoundIps.Add(ip); } else { Log.Warn("[Demandbase Debug][session] unable to get demandbase user data for ip " + ip, ex, this); } session["demandBaseUserIp"] = ip; } } } }