public bool MatchesVisitor(string definition) { Mandate.ParameterNotNullOrEmpty(definition, "definition"); IList <CountrySetting> definedCountries; try { definedCountries = JsonConvert.DeserializeObject <IList <CountrySetting> >(definition); } catch (JsonReaderException) { throw new ArgumentException(string.Format("Provided definition is not valid JSON: {0}", definition)); } var ip = _ipProvider.GetIp(); ip = "2.37.150.125"; if (!string.IsNullOrEmpty(ip)) { var country = _countryGeoLocationProvider.GetCountryFromIp(ip); if (!string.IsNullOrEmpty(country)) { return(definedCountries .Any(x => string.Equals(x.Code, country, StringComparison.InvariantCultureIgnoreCase))); } } return(false); }
public bool MatchesVisitor(string definition) { if (string.IsNullOrEmpty(definition)) { throw new ArgumentNullException(nameof(definition)); } RegionSetting regionSetting; try { regionSetting = JsonConvert.DeserializeObject <RegionSetting>(definition); } catch (JsonReaderException) { throw new ArgumentException($"Provided definition is not valid JSON: {definition}"); } var ip = _ipProvider.GetIp(); if (!string.IsNullOrEmpty(ip)) { var country = _geoLocationProvider.GetCountryFromIp(ip); if (country != null) { if (regionSetting.Match == GeoLocationSettingMatch.CouldNotBeLocated) { // We can't locate, so return false. return(false); } var matchedCountry = string.Equals(regionSetting.CountryCode, country.Code, StringComparison.InvariantCultureIgnoreCase); var matchedRegion = false; if (matchedCountry) { var region = _geoLocationProvider.GetRegionFromIp(ip); if (region != null) { matchedRegion = regionSetting.Names .Intersect(region.GetAllNames(), StringComparer.OrdinalIgnoreCase) .Any(); } } switch (regionSetting.Match) { case GeoLocationSettingMatch.IsLocatedIn: return(matchedRegion); case GeoLocationSettingMatch.IsNotLocatedIn: return(!matchedRegion); default: return(false); } } } return(regionSetting.Match == GeoLocationSettingMatch.CouldNotBeLocated); }
public string GetCountryCode() { var ip = _ipProvider.GetIp(); if (string.IsNullOrEmpty(ip)) { return(string.Empty); } var country = _geoLocationProvider.GetCountryFromIp(ip); return(country != null ? country.Code : string.Empty); }
public bool MatchesVisitor(string definition) { if (string.IsNullOrEmpty(definition)) { throw new ArgumentNullException(nameof(definition)); } ContinentSetting countrySetting; try { countrySetting = JsonConvert.DeserializeObject <ContinentSetting>(definition); } catch (JsonReaderException) { throw new ArgumentException($"Provided definition is not valid JSON: {definition}"); } var ip = _ipProvider.GetIp(); if (!string.IsNullOrEmpty(ip)) { var country = _geoLocationProvider.GetContinentFromIp(ip); if (country != null) { if (countrySetting.Match == GeoLocationSettingMatch.CouldNotBeLocated) { // We can't locate, so return false. return(false); } var matchedContinent = countrySetting.Codes .Any(x => string.Equals(x, country.Code, StringComparison.InvariantCultureIgnoreCase)); switch (countrySetting.Match) { case GeoLocationSettingMatch.IsLocatedIn: return(matchedContinent); case GeoLocationSettingMatch.IsNotLocatedIn: return(!matchedContinent); default: return(false); } } } return(countrySetting.Match == GeoLocationSettingMatch.CouldNotBeLocated); }
public bool MatchesVisitor(string definition) { Mandate.ParameterNotNullOrEmpty(definition, "definition"); CountrySetting countrySetting; try { countrySetting = JsonConvert.DeserializeObject <CountrySetting>(definition); } catch (JsonReaderException) { throw new ArgumentException($"Provided definition is not valid JSON: {definition}"); } var ip = _ipProvider.GetIp(); if (!string.IsNullOrEmpty(ip)) { var country = _countryGeoLocationProvider.GetCountryFromIp(ip); if (!string.IsNullOrEmpty(country)) { var matchedCountry = countrySetting.Codes .Any(x => string.Equals(x, country, StringComparison.InvariantCultureIgnoreCase)); switch (countrySetting.Match) { case CountrySettingMatch.IsLocatedIn: return(matchedCountry); case CountrySettingMatch.IsNotLocatedIn: return(!matchedCountry); default: return(false); } } } return(false); }