Example #1
0
        public async Task <RecaptchaResponse> ValidateRecaptcha(
            HttpRequest request,
            string secretKey)
        {
            var client = _httpClientFactory.GetOrCreateHttpClient(new Uri("https://www.google.com/"));

            var response = request.Form["g-recaptcha-response"];

            string result = await client.GetStringAsync(
                string.Format("recaptcha/api/siteverify?secret={0}&response={1}",
                              secretKey,
                              response)
                );

            var captchaResponse = JsonConvert.DeserializeObject <RecaptchaResponse>(result);

            return(captchaResponse);
        }
Example #2
0
        public async Task TrackPageView(
            string profileId,
            string clientId,
            string userId,
            string hostName,
            string path,
            string title,
            string ipAddress       = "",
            string userAgent       = "",
            string campaignSource  = "",
            string campaignMedium  = "",
            string campaignName    = "",
            string campaignTerm    = "",
            string campaignContent = ""

            )
        {
            if (string.IsNullOrWhiteSpace(profileId))
            {
                _log.LogWarning("ignoring call to TrackPageView because profileid not provided");
                return;
            }

            if (string.IsNullOrWhiteSpace(hostName))
            {
                _log.LogWarning("ignoring call to TrackPageView because hostName not provided");
                return;
            }

            if (string.IsNullOrWhiteSpace(path))
            {
                _log.LogWarning("ignoring call to TrackPageView because path not provided");
                return;
            }

            if (string.IsNullOrWhiteSpace(title))
            {
                _log.LogWarning("ignoring call to TrackPageView because title not provided");
                return;
            }

            var keyValues = new List <KeyValuePair <string, string> >();

            keyValues.Add(new KeyValuePair <string, string>("v", version));
            keyValues.Add(new KeyValuePair <string, string>("tid", profileId));

            if (!string.IsNullOrWhiteSpace(userId))
            {
                keyValues.Add(new KeyValuePair <string, string>("uid", userId));
            }
            else if (!string.IsNullOrWhiteSpace(clientId))
            {
                keyValues.Add(new KeyValuePair <string, string>("cid", clientId));
            }
            else
            {
                keyValues.Add(new KeyValuePair <string, string>("uid", Guid.NewGuid().ToString()));
            }

            keyValues.Add(new KeyValuePair <string, string>("t", "pageview"));
            keyValues.Add(new KeyValuePair <string, string>("dh", hostName));
            keyValues.Add(new KeyValuePair <string, string>("dp", path));
            keyValues.Add(new KeyValuePair <string, string>("dt", title));

            if (!string.IsNullOrWhiteSpace(campaignSource))
            {
                keyValues.Add(new KeyValuePair <string, string>("cs", campaignSource));
            }

            if (!string.IsNullOrWhiteSpace(campaignMedium))
            {
                keyValues.Add(new KeyValuePair <string, string>("cm", campaignMedium));
            }

            if (!string.IsNullOrWhiteSpace(campaignName))
            {
                keyValues.Add(new KeyValuePair <string, string>("cn", campaignName));
            }

            if (!string.IsNullOrWhiteSpace(campaignTerm))
            {
                keyValues.Add(new KeyValuePair <string, string>("ck", campaignTerm));
            }

            if (!string.IsNullOrWhiteSpace(campaignContent))
            {
                keyValues.Add(new KeyValuePair <string, string>("cc", campaignContent));
            }

            if (!string.IsNullOrWhiteSpace(userAgent))
            {
                keyValues.Add(new KeyValuePair <string, string>("ua", userAgent));
            }

            if (!string.IsNullOrWhiteSpace(ipAddress))
            {
                keyValues.Add(new KeyValuePair <string, string>("uip", ipAddress));
            }

            var content = new FormUrlEncodedContent(keyValues);

            var client = _httpClientFactory.GetOrCreateHttpClient(new Uri(gaUrl));

            try
            {
                var response = await client.PostAsync(
                    "collect",
                    content).ConfigureAwait(false);

                if (response.IsSuccessStatusCode)
                {
                    _log.LogDebug($"success posting pageview to google analytics");
                }
                else
                {
                    string responseBody = await response.Content.ReadAsStringAsync();

                    var logmessage = $"failed to send pageview data to google analytics, response was: { responseBody }";
                    _log.LogWarning(logmessage);
                }
            }
            catch (Exception ex)
            {
                _log.LogError("error posting to google analytics: " + ex.Message + " stacktrace: " + ex.StackTrace);
            }
        }