示例#1
0
        private async Task <bool> IsValid(string token, HttpContext context)
        {
            if (string.IsNullOrEmpty(token))
            {
                throw new ValidationException("Invalid token.");
            }

            if (DefaultToken.IsNotNullOrEmpty() && token == DefaultToken)
            {
                return(true);
            }

            ReadConfig(context);


            using var httpClient = new HttpClient();


            var stringAsync = await httpClient
                              .GetStringAsync(new Uri($"{CaptchaUri}?secret={SecretKey}&response={token}"))
                              .ConfigureAwait(false);

            var serializerSettings = new JsonSerializerOptions
            {
                IgnoreNullValues = true
            };


            var captchaResponse = JsonSerializer.Deserialize <dynamic>(stringAsync, serializerSettings);

            if (captchaResponse is null)
            {
                return(false);
            }

            if (captchaResponse.Success & AntiForgery && HostName.IsNotNullOrEmpty() &&
                !captchaResponse.Hostname.Equals(HostName))
            {
                throw new ValidationException(
                          "Captcha hostname and request hostname do not match. Please review anti forgery settings.");
            }

            return(captchaResponse.Success);
        }