async public Task <string> AddSuspicousUserAndGenerateCaptchaCodeAsync(string username) { var suspiciousUser = SuspiciousUser.FromStringOrDefault(await _cache.GetStringAsync(username), username); suspiciousUser.CountFailes++; Random rand = new Random(); int maxRand = _options.CaptchaCodeLetters.Length - 1; StringBuilder sb = new StringBuilder(); for (int i = 0; i < _options.CaptchaCodeLength; i++) { int index = rand.Next(maxRand); sb.Append(_options.CaptchaCodeLetters[index]); } string code = sb.ToString(); suspiciousUser.CaptchaCode = code; suspiciousUser.TimeStamp = DateTime.Now; await _cache.SetStringAsync(username, suspiciousUser.ToString()); return(code); }
async public Task AddSuspiciousUserAsync(string username) { var suspiciousUser = SuspiciousUser.FromStringOrDefault(await _cache.GetStringAsync(username), username); suspiciousUser.TimeStamp = DateTime.Now; suspiciousUser.CountFailes++; await _cache.SetStringAsync(username, suspiciousUser.ToString()); }
async public Task RemoveSuspiciousUserAsync(string username) { var suspiciousUser = SuspiciousUser.FromString(await _cache.GetStringAsync(username)); if (suspiciousUser != null) { await _cache.RemoveAsync(username); } }
async public Task <bool> IsSuspiciousUserAsync(string username) { if (String.IsNullOrWhiteSpace(username)) { throw new ArgumentException("Username required"); } var suspiciousUser = SuspiciousUser.FromString(await _cache.GetStringAsync(username)); if (suspiciousUser != null) { var lastSet = suspiciousUser.TimeStamp.ToUniversalTime(); if ((DateTime.UtcNow - lastSet).TotalMinutes >= _options.RembemberSuspiciousUserTotalMinutes) { await RemoveSuspiciousUserAsync(username); return(false); } return(suspiciousUser.CountFailes >= _options.MaxFailCount); } return(false); }
async public Task <bool> VerifyCaptchaCodeAsync(string username, string code) { var suspiciousUser = SuspiciousUser.FromStringOrDefault(await _cache.GetStringAsync(username), username); return(code != null && code.Equals(suspiciousUser.CaptchaCode, StringComparison.InvariantCultureIgnoreCase)); }