public void TrackSession() { var key = _config.CookieKeyForTrackingIfSessionAlreadyTracked; // Check if session cookie present. var sessionCookieExists = _cookieProvider.CookieExists(key); if (!sessionCookieExists) { // If not, create or update the number of visits cookie. var value = _cookieProvider.GetCookieValue(_config.CookieKeyForTrackingNumberOfVisits); if (!string.IsNullOrEmpty(value)) { value = int.TryParse(value, out int cookieValue) ? (cookieValue + 1).ToString() : "1"; } else { value = "1"; } var expires = _dateTimeProvider.GetCurrentDateTime().AddDays(_config.NumberOfVisitsTrackingCookieExpiryInDays); _cookieProvider.SetCookie(key, value, expires); // Set the session cookie so we don't keep updating on each request _cookieProvider.SetCookie(_config.CookieKeyForTrackingIfSessionAlreadyTracked, "1"); } }
public bool MatchesVisitor(string definition) { Mandate.ParameterNotNullOrEmpty(definition, "definition"); CookieSetting cookieSetting; try { cookieSetting = JsonConvert.DeserializeObject <CookieSetting>(definition); } catch (JsonReaderException) { throw new ArgumentException($"Provided definition is not valid JSON: {definition}"); } if (string.IsNullOrEmpty(cookieSetting.Key)) { throw new ArgumentNullException("key", "Cookie key not set"); } var cookieExists = _cookieProvider.CookieExists(cookieSetting.Key); var cookieValue = string.Empty; if (cookieExists) { cookieValue = _cookieProvider.GetCookieValue(cookieSetting.Key); } switch (cookieSetting.Match) { case CookieSettingMatch.Exists: return(cookieExists); case CookieSettingMatch.DoesNotExist: return(!cookieExists); case CookieSettingMatch.MatchesValue: return(cookieExists && MatchesValue(cookieValue, cookieSetting.Value)); case CookieSettingMatch.ContainsValue: return(cookieExists && ContainsValue(cookieValue, cookieSetting.Value)); case CookieSettingMatch.GreaterThanValue: case CookieSettingMatch.GreaterThanOrEqualToValue: case CookieSettingMatch.LessThanValue: case CookieSettingMatch.LessThanOrEqualToValue: return(cookieExists && CompareValues(cookieValue, cookieSetting.Value, GetComparison(cookieSetting.Match))); case CookieSettingMatch.MatchesRegex: return(cookieExists && MatchesRegex(cookieValue, cookieSetting.Value)); case CookieSettingMatch.DoesNotMatchRegex: return(cookieExists && !MatchesRegex(cookieValue, cookieSetting.Value)); default: return(false); } }