Пример #1
0
        public override void RenderControl(HtmlTextWriter writer)
        {
            NoCaptcha nc = (string.IsNullOrEmpty(SiteKey) || string.IsNullOrEmpty(SecretKey)) ?
                           new NoCaptcha() :
                           new NoCaptcha(SiteKey, SecretKey);

            writer.AddAttribute(HtmlTextWriterAttribute.Class, "g-recaptcha");
            writer.AddAttribute("data-sitekey", nc.SiteKey);
            if (UseSecureToken)
            {
                writer.AddAttribute("data-stoken", nc.SecureToken);
            }
            writer.AddAttribute("data-theme", Theme.ToString().ToLower());
            writer.AddAttribute("data-type", Type.ToString().ToLower());
            writer.AddAttribute("data-size", Size.ToString().ToLower());

            writer.RenderBeginTag(HtmlTextWriterTag.Div);
            writer.RenderEndTag();

            if (!IsValid && ((Page)HttpContext.Current.Handler).IsPostBack)
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Class, ErrorCssClass);
                writer.RenderBeginTag(HtmlTextWriterTag.Div);
                writer.WriteEncodedText(ErrorMessage);
                writer.RenderEndTag();
            }
        }
Пример #2
0
        protected override bool EvaluateIsValid()
        {
            NoCaptcha nc = (string.IsNullOrEmpty(SiteKey) || string.IsNullOrEmpty(SecretKey)) ?
                           new NoCaptcha() :
                           new NoCaptcha(SiteKey, SecretKey);

            string clientResponse = GetClientResponse();

            if (string.IsNullOrEmpty(clientResponse))
            {
                return(false);
            }
            var result = nc.Validate(clientResponse, GetClientIP());

            IsValid = result.Succeeded;
            if (IsValid)
            {
                return(true);
            }

            List <string> errors = new List <string>();

            if (result.ErrorMessages == null)
            {
                errors.Add("Unspecified Error");
            }
            else
            {
                foreach (string error in result.ErrorMessages)
                {
                    switch (error)
                    {
                    case "missing-input-secret":
                        errors.Add("The secret parameter is missing");
                        break;

                    case "invalid-input-secret":
                        errors.Add("The secret parameter is invalid or malformed");
                        break;

                    case "missing-input-response":
                        errors.Add("The response parameter is missing");
                        break;

                    case "invalid-input-response":
                        errors.Add("The response parameter is invalid or malformed");
                        break;

                    default:
                        errors.Add(error);
                        break;
                    }
                }
            }

            ErrorMessage = string.Join(", ", errors);

            return(false);
        }