Exemplo n.º 1
0
        /// <summary>
        /// Logs an authentication error to the database to prevent future requests until the wait time has lapsed
        /// </summary>
        /// <param name="ipAddress"></param>
        /// <param name="lastAttemptDate"></param>
        /// <param name="SecondsUntilNextAttempt"></param>
        public static void LogAuthorizationError(string ipAddress, out DateTime lastAttemptDate, out long SecondsUntilNextAttempt)
        {
            Attempt a = null;
            DbContext dbContext = new DbContext();
            lastAttemptDate = DateTime.Now;

            var allAttempts = dbContext.Attempts.Where(i => i.IPAddress == ipAddress);

            if (allAttempts.Count() == 0)
            {
                a = new Attempt();
                a.DateOfLastAttempt = lastAttemptDate;
                a.IPAddress = ipAddress;
                a.NumberOfAttemptsSinceLastSuccess = 1;
                a.SecondsUntilNextAttempt = Convert.ToInt64(FailWaitExtenderLength);
                dbContext.Attempts.InsertOnSubmit(a);
            }
            else
            {
                a = allAttempts.First();
                a.DateOfLastAttempt = lastAttemptDate;
                a.NumberOfAttemptsSinceLastSuccess++;
                var power = Math.Pow(FailWaitExtenderLength, a.NumberOfAttemptsSinceLastSuccess);

                a.SecondsUntilNextAttempt = Convert.ToInt64(power);
            }
            dbContext.SubmitChanges();

            //return seconds until when they can try again
            SecondsUntilNextAttempt = a.SecondsUntilNextAttempt;
        }
 partial void UpdateAttempt(Attempt instance);
 partial void DeleteAttempt(Attempt instance);
 partial void InsertAttempt(Attempt instance);