private void RenewSessions(string sessionKey, string authKey, DateTimeOffset?expiresUtc)
        {
            var options = new DistributedCacheEntryOptions();

            if (expiresUtc.HasValue)
            {
                options.SetAbsoluteExpiration(expiresUtc.Value);
            }

            if (_httpContextAccessor.HttpContext.Request.Headers.TryGetValue("user-agent", out var ua))
            {
                var state = "unknown";
                var city  = "unknown";

                try
                {
                    var response = _ipClient.City(_httpContextAccessor.HttpContext.Connection.RemoteIpAddress);

                    state = response.MostSpecificSubdivision.Name; // 'Minnesota'
                    city  = response.City.Name;
                } catch (AddressNotFoundException ex) {
                    if (ex.Message.Contains("reserved IP address"))
                    {
                        city  = "developing";
                        state = "localhost";
                    }
                }

                var client  = _parser.Parse(ua);
                var session = new Session(client, state, city, authKey);

                var logins = RetrieveLoginSessions(sessionKey);
                logins.Locations.Add(session);

                WriteSessionCache(sessionKey, logins, options);
            }
        }
Esempio n. 2
0
        public void CorrectlyFormattedCityResponseShouldDeserializeIntoResponseObject()
        {
            var restResponse = new RestResponse
            {
                Content     = OMNI_BODY,
                ContentType = "application/json",
                ResponseUri = new Uri("http://foo.com/omni/1.2.3.4"),
                StatusCode  = (HttpStatusCode)200
            };

            restResponse.ContentLength = restResponse.Content.Length;

            var restClient = MockRepository.GenerateStub <IRestClient>();

            restClient.Stub(r => r.Execute(Arg <IRestRequest> .Is.Anything)).Return(restResponse);

            var wsc = new WebServiceClient(0, "abcdef", new List <string> {
                "en"
            });
            var result = wsc.City("1.2.3.4", restClient);

            Assert.That(result, Is.Not.Null);
            Assert.That(result, Is.InstanceOf <CityResponse>());
        }
        public void CorrectlyFormattedCityResponseShouldDeserializeIntoResponseObject()
        {
            var restResponse = new RestResponse
            {
                Content = OMNI_BODY,
                ContentType = "application/json",
                ResponseUri = new Uri("http://foo.com/omni/1.2.3.4"),
                StatusCode = (HttpStatusCode)200
            };

            restResponse.ContentLength = restResponse.Content.Length;

            var restClient = MockRepository.GenerateStub<IRestClient>();

            restClient.Stub(r => r.Execute(Arg<IRestRequest>.Is.Anything)).Return(restResponse);

            var wsc = new WebServiceClient(0, "abcdef", new List<string> { "en" });
            var result = wsc.City("1.2.3.4", restClient);

            Assert.That(result, Is.Not.Null);
            Assert.That(result, Is.InstanceOf<CityResponse>());
        }