public async Task <bool> IsCaptchaValidAsync()
        {
            if (!(_validatorService.HasRequestValidCaptchaEntry(Language.English, DisplayMode.ShowDigits)))
            {
                // If the user has a captcha cookie.
                if (_cookieManager.HasCookie(CookieKeys.ValidateCaptchaCookie))
                {
                    bool validToken = await _validTokens.DoesTokenExistAsync(_cookieManager.CookieValue(CookieKeys.ValidateCaptchaCookie));

                    if (!validToken)
                    {
                        // Has a Cached Cookie but an invalid Hash.
                        return(false);
                    }
                    // Valid Cookie with a Valid Hash (Cache Hit)
                    return(true);
                }
                else
                {
                    // No Cached Cookie & Wrong Captcha Code.
                    return(false);
                }
            }
            await _validTokens.CleanUpUnusedTokensAsync();

            // Captcha completed successfully by user.
            return(true);
        }
 public async Task <bool> TokenValidAsync(string userId)
 {
     if (await _context.TwoFactorTokens.FirstOrDefaultAsync(u => u.UserId.Equals(userId)) == null || !_cookies.HasCookie(CookieKeys.TwoFactorAuthorizeCookie))
     {
         return(false);
     }
     if (GoliathHash.ValidateStringSHA256(_cookies.CookieValue(CookieKeys.TwoFactorAuthorizeCookie)))
     {
         if (GoliathHash.HashStringSHA256((await _context.TwoFactorTokens.FirstOrDefaultAsync(u => u.UserId.Equals(userId))).AuthorizeToken).Equals(_cookies.CookieValue(CookieKeys.TwoFactorAuthorizeCookie)))
         {
             return(true);
         }
     }
     return(false);
 }