Exemplo n.º 1
0
        /// <summary>
        ///     Determines whether the captcha is valid, and write error message if need.
        /// </summary>
        /// <param name="controller">
        ///     The specified <see cref="ControllerBase" />.
        /// </param>
        /// <param name="parameterContainer">
        ///     The specified <see cref="IParameterContainer" />.
        /// </param>
        /// <returns>
        ///     <c>True</c> if the captcha is valid; otherwise, <c>false</c>.
        /// </returns>
        public virtual bool ValidateCaptcha(ControllerBase controller, IParameterContainer parameterContainer)
        {
            Validate.ArgumentNotNull(controller, "controller");
            Validate.ArgumentNotNull(parameterContainer, "parameterContainer");
            if (IntelligencePolicy != null)
            {
                var isValid = IntelligencePolicy.IsValid(this, controller, parameterContainer);
                if (isValid.HasValue)
                {
                    if (isValid.Value)
                    {
                        return(true);
                    }
                    WriteError(controller, parameterContainer);
                    return(false);
                }
            }
            ValueProviderResult tokenValue = controller.ValueProvider.GetValue(TokenElementName);
            ValueProviderResult inputText  = controller.ValueProvider.GetValue(InputElementName);

            if (tokenValue != null && !string.IsNullOrEmpty(tokenValue.AttemptedValue) && inputText != null)
            {
                ICaptchaValue captchaValue = StorageProvider.GetValue(tokenValue.AttemptedValue, TokenType.Validation);
                if (captchaValue != null && !string.IsNullOrEmpty(inputText.AttemptedValue) &&
                    captchaValue.IsEqual(inputText.AttemptedValue))
                {
                    return(true);
                }
            }
            WriteError(controller, parameterContainer);
            return(false);
        }
Exemplo n.º 2
0
        /// <summary>
        ///     Creates a new <see cref="IBuildInfoModel" /> for update a captcha.
        /// </summary>
        /// <param name="parameterContainer">
        ///     The specified <see cref="IParameterContainer" />.
        /// </param>
        /// <returns>
        ///     An instance of <see cref="IUpdateInfoModel" />.
        /// </returns>
        public IUpdateInfoModel Update(IParameterContainer parameterContainer)
        {
            Validate.ArgumentNotNull(parameterContainer, "parameterContainer");
            string token;

            parameterContainer.TryGet(TokenParameterName, out token, null);
            if (string.IsNullOrEmpty(token))
            {
                throw new KeyNotFoundException("The key is to generate not found.");
            }
            ICaptchaValue captchaValue = StorageProvider.GetValue(token, TokenType.Validation);

            if (captchaValue == null)
            {
                throw new ArgumentException("The key is to update incorrect.");
            }

            HttpRequestBase request;

            if (!parameterContainer.TryGet(RequestParameterContainer.HttpRequestParameterKey, out request) ||
                request == null)
            {
                throw new InvalidOperationException(
                          "The parameterContainer does not contain a HttpRequestBase with key RequestParameterContainer.HttpRequestParameterKey.");
            }
            KeyValuePair <string, ICaptchaValue> captchaPair = CreateCaptchaPair(parameterContainer, captchaValue);
            string newUrl = ImageUrlFactory(new UrlHelper(request.RequestContext), captchaPair);

            StorageProvider.Add(captchaPair);
            return(new DefaultUpdateInfoModel(TokenElementName, captchaPair.Key, newUrl, ImageElementName));
        }
Exemplo n.º 3
0
        /// <summary>
        ///     Creates a new <see cref="IDrawingModel" /> for drawing a captcha.
        /// </summary>
        /// <param name="parameterContainer">
        ///     The specified <see cref="IParameterContainer" />.
        /// </param>
        /// <returns>
        ///     An instance of <see cref="IDrawingModel" />.
        /// </returns>
        public virtual IDrawingModel GetDrawingModel(IParameterContainer parameterContainer)
        {
            Validate.ArgumentNotNull(parameterContainer, "parameterContainer");
            string token;

            if (!parameterContainer.TryGet(TokenParameterName, out token) || string.IsNullOrEmpty(token))
            {
                throw new KeyNotFoundException("The key is to generate not found.");
            }
            ICaptchaValue captchaValue = StorageProvider.GetValue(token, TokenType.Drawing);

            if (captchaValue == null)
            {
                throw new ArgumentException("The key is to generate incorrect.");
            }
            return(DrawingModelFactory(parameterContainer, captchaValue));
        }
Exemplo n.º 4
0
        /// <summary>
        ///     Serializes the <see cref="ICaptchaValue" />, to the given string.
        /// </summary>
        /// <param name="captchaValue">
        ///     The specified <see cref="ICaptchaValue" />.
        /// </param>
        /// <returns>The result string.</returns>
        protected virtual string Serialize(ICaptchaValue captchaValue)
        {
            Validate.ArgumentNotNull(captchaValue, "captchaValue");
            using (var pdb = new Rfc2898DeriveBytes(Password, Salt))
            {
                string value = captchaValue.GetType().FullName + Separator + captchaValue.Serialize();
                byte[] bytes = Encoding.Unicode.GetBytes(value);
                using (var ms = new MemoryStream())
                {
                    Rijndael alg = Rijndael.Create();

                    alg.Key = pdb.GetBytes(32);
                    alg.IV  = pdb.GetBytes(16);

                    using (var cs = new CryptoStream(ms, alg.CreateEncryptor(), CryptoStreamMode.Write))
                    {
                        cs.Write(bytes, 0, bytes.Length);
                    }
                    return(Convert.ToBase64String(ms.ToArray()));
                }
            }
        }
Exemplo n.º 5
0
        /// <summary>
        ///     Generates a specified <see cref="KeyValuePair{TKey,TValue}" /> for a captcha.
        /// </summary>
        /// <param name="parameterContainer">
        ///     The specified <see cref="IParameterContainer" />.
        /// </param>
        /// <param name="oldValue">The old value if any.</param>
        /// <returns>
        ///     An instance of <see cref="KeyValuePair{TKey,TValue}" />.
        /// </returns>
        protected virtual KeyValuePair <string, ICaptchaValue> CreateCaptchaPair(IParameterContainer parameterContainer,
                                                                                 ICaptchaValue oldValue)
        {
            if (parameterContainer.IsContains(MathCaptchaAttribute))
            {
                return(MathCaptchaPairFactory());
            }

            int length;

            if (oldValue != null)
            {
                length = oldValue.CaptchaText.Length;
            }
            else if (!parameterContainer.TryGet(LengthAttribute, out length))
            {
                throw new ArgumentException("Parameter is not specified for the length of the captcha.");
            }
            if (length <= 0)
            {
                throw new ArgumentException("The length parameter can not be <= 0.");
            }
            return(PlainCaptchaPairFactory(length));
        }
Exemplo n.º 6
0
 /// <summary>
 ///     Creates a new <see cref="IDrawingModel" /> for drawing a captcha.
 /// </summary>
 /// <param name="parameterContainer">
 ///     The specified <see cref="IParameterContainer" />.
 /// </param>
 /// <param name="captchaValue">
 ///     The specified <see cref="ICaptchaValue" />.
 /// </param>
 /// <returns>
 ///     An instance of <see cref="IDrawingModel" />.
 /// </returns>
 private static IDrawingModel CreateDrawingModel(IParameterContainer parameterContainer,
                                                 ICaptchaValue captchaValue)
 {
     return(new DefaultDrawingModel(captchaValue.CaptchaText));
 }