Пример #1
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));
        }
 /// <summary>
 ///     Gets the value associated with the specified key.
 /// </summary>
 /// <typeparam name="T">The type of value.</typeparam>
 /// <param name="key">The specified key.</param>
 /// <param name="value">An instance of T.</param>
 /// <returns>
 ///     <c>True</c> if the value is found in the <see cref="IParameterContainer" />; otherwise, <c>false</c>.
 /// </returns>
 public bool TryGet <T>(string key, out T value)
 {
     if (_firstContainer.IsContains(key))
     {
         return(_firstContainer.TryGet(key, out value));
     }
     if (_secondContainer.IsContains(key))
     {
         return(_secondContainer.TryGet(key, out value));
     }
     value = default(T);
     return(false);
 }
Пример #3
0
        /// <summary>
        ///     Creates an <see cref="IBuildInfoModel" /> for the specified <see cref="KeyValuePair{TKey,TValue}" />.
        /// </summary>
        /// <param name="htmlHelper">
        ///     The specified <see cref="HtmlHelper" />.
        /// </param>
        /// <param name="parameterContainer">
        ///     The specified <see cref="IParameterContainer" />.
        /// </param>
        /// <param name="captchaPair">
        ///     The specified <see cref="KeyValuePair{TKey,TValue}" />.
        /// </param>
        /// <param name="imgUrl">The specified image url.</param>
        /// <param name="refreshUrl">The specified refresh url.</param>
        /// <returns>
        ///     An instance of <see cref="IBuildInfoModel" />.
        /// </returns>
        protected virtual IBuildInfoModel CreateBuildInfo(HtmlHelper htmlHelper, IParameterContainer parameterContainer,
                                                          KeyValuePair <string, ICaptchaValue> captchaPair, string imgUrl,
                                                          string refreshUrl)
        {
            string requiredText = null;
            string refreshText;
            string inputText;

            parameterContainer.TryGet(RefreshTextAttribute, out refreshText, "Refresh");
            bool findInputText = parameterContainer.TryGet(InputTextAttribute, out inputText);
            bool isRequired    = parameterContainer.IsContains(IsRequiredAttribute);

            if (isRequired)
            {
                parameterContainer.TryGet(RequiredMessageAttribute, out requiredText, "This is a required field.");
            }

            IBuildInfoModel buildInfo;

            if (parameterContainer.IsContains(MathCaptchaAttribute))
            {
                buildInfo = new MathBuildInfoModel(parameterContainer, TokenParameterName, MathCaptchaAttribute, isRequired, requiredText,
                                                   refreshText, findInputText ? inputText : "The answer is", htmlHelper,
                                                   InputElementName, TokenElementName,
                                                   ImageElementName, imgUrl, refreshUrl, captchaPair.Key);
            }
            else
            {
                buildInfo = new DefaultBuildInfoModel(parameterContainer, TokenParameterName, requiredText, isRequired,
                                                      refreshText, findInputText ? inputText : "Input symbols",
                                                      htmlHelper,
                                                      InputElementName, ImageElementName, TokenElementName, refreshUrl,
                                                      imgUrl,
                                                      captchaPair.Key);
            }

            //If it a partial view.
            if (parameterContainer.IsContains(PartialViewNameAttribute))
            {
                ViewDataDictionary viewData;
                parameterContainer.TryGet(PartialViewDataAttribute, out viewData);
                string scriptPartialView;
                parameterContainer.TryGet(ScriptPartialViewNameAttribute, out scriptPartialView);

                return(new PartialBuildInfoModel(htmlHelper, buildInfo,
                                                 parameterContainer.Get <string>(PartialViewNameAttribute),
                                                 scriptPartialView, viewData));
            }
            return(buildInfo);
        }
Пример #4
0
        /// <summary>
        ///     Writes an error message.
        /// </summary>
        /// <param name="controllerBase">
        ///     The specified <see cref="ControllerBase" />.
        /// </param>
        /// <param name="parameterContainer">
        ///     The specified <see cref="IParameterContainer" />.
        /// </param>
        protected virtual void WriteError(ControllerBase controllerBase, IParameterContainer parameterContainer)
        {
            string errorText;

            parameterContainer.TryGet(ErrorAttribute, out errorText, "The captcha is not valid");
            controllerBase.ViewData.ModelState.AddModelError(InputElementName, errorText);
            controllerBase.ViewData[CaptchaNotValidViewDataKey] = true;
        }
Пример #5
0
        private static ICaptchaManager GetCaptchaManager(IParameterContainer parameterContainer)
        {
            int numberOfCaptcha;
            if (parameterContainer.TryGet(MultipleParameterKey, out numberOfCaptcha))
                return CaptchaManagers.GetOrAdd(numberOfCaptcha, CreateCaptchaManagerByNumber);

            //If not found parameter return default manager.
            return CaptchaUtils.CaptchaManager;
        }
        private static ICaptchaManager GetCaptchaManager(IParameterContainer parameterContainer)
        {
            int numberOfCaptcha;

            if (parameterContainer.TryGet(MultipleParameterKey, out numberOfCaptcha))
            {
                return(CaptchaManagers.GetOrAdd(numberOfCaptcha, CreateCaptchaManagerByNumber));
            }

            //If not found parameter return default manager.
            return(CaptchaUtils.CaptchaManager);
        }
Пример #7
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));
        }
Пример #8
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));
        }