/// <summary>
        ///     Creates a html string to represent the input element.
        /// </summary>
        /// <param name="buildInfo">
        ///     The specified <see cref="IBuildInfoModel" />.
        /// </param>
        /// <returns>The html string with the input element.</returns>
        protected virtual string GenerateInputElement(IBuildInfoModel buildInfo)
        {
            IDictionary <string, object> attributes = new Dictionary <string, object>();

            if (buildInfo.IsRequired)
            {
                attributes.Add(@"data-val", "true");
                attributes.Add("data-val-required", buildInfo.RequiredMessage);
            }
            attributes.Add("autocomplete", "off");
            attributes.Add("autocorrect", "off");
            attributes.Add("class", "form-control placeholder-no-fix");
            MvcHtmlString input = buildInfo.HtmlHelper.TextBox(buildInfo.InputElementId, null, attributes);

            var  validationMessage = string.Empty;
            bool addSpan;

            if (buildInfo.ParameterContainer.TryGet(DefaultCaptchaManager.IsNeedValidationSpanAttribute, out addSpan) && addSpan)
            {
                validationMessage = string.Format(
                    @"<span class=""field-validation-valid"" data-valmsg-for=""{0}"" data-valmsg-replace=""true""></span>",
                    buildInfo.InputElementId);
            }

            return(string.Format("{0}{1}", input, validationMessage));
        }
Example #2
0
        /// <summary>
        ///     Creates a new captcha with the specified arguments.
        /// </summary>
        /// <param name="htmlHelper">
        ///     The specified <see cref="HtmlHelper" />.
        /// </param>
        /// <param name="parameters">The specified attributes.</param>
        /// <returns>
        ///     An instance of <see cref="ICaptcha" />.
        /// </returns>
        public static ICaptcha GenerateCaptcha(HtmlHelper htmlHelper, IList <ParameterModel> parameters)
        {
            var             parameterContainer = new ParameterModelContainer(parameters);
            IBuildInfoModel buildInfoModel     = CaptchaManagerFactory(parameterContainer)
                                                 .GenerateNew(htmlHelper, parameterContainer);

            return(BuilderProviderFactory(parameterContainer).GenerateCaptcha(buildInfoModel));
        }
        /// <summary>
        ///     Creates a html string to represent the image.
        /// </summary>
        /// <param name="buildInfoModel">
        ///     The specified <see cref="IBuildInfoModel" />.
        /// </param>
        /// <returns>The html string with the image.</returns>
        protected virtual RefreshButton GenerateCaptchaImage(IBuildInfoModel buildInfoModel)
        {
            string functionName = string.Format("______{0}________()", Guid.NewGuid().ToString("N"));
            string updateScript = string.Format(UpdateScript, buildInfoModel.ImageElementId, buildInfoModel.RefreshUrl,
                                                buildInfoModel.TokenParameterName,
                                                buildInfoModel.TokenElementId, functionName);
            string imageString = string.Format(CaptchaFormat, buildInfoModel.ImageElementId, buildInfoModel.ImageUrl, functionName, buildInfoModel.RefreshButtonText);

            return(new RefreshButton(imageString, updateScript));
        }
Example #4
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="CaptchaModel" /> class.
 /// </summary>
 public CaptchaModel(IBuildInfoModel buildInfoModel, string markup, string script)
 {
     Validate.ArgumentNotNull(buildInfoModel, "buildInfoModel");
     Validate.ArgumentNotNull(markup, "markup");
     _markup = markup;
     if (script == null)
     {
         script = string.Empty;
     }
     _script   = script;
     BuildInfo = buildInfoModel;
 }
Example #5
0
        /// <summary>
        ///     Creates a new captcha using the specified <see cref="IBuildInfoModel" />.
        /// </summary>
        /// <param name="buildInfoModel">
        ///     The specified <see cref="IBuildInfoModel" />.
        /// </param>
        /// <returns>An instance of <see cref="ICaptcha"/>.</returns>
        public virtual ICaptcha Build(IBuildInfoModel buildInfoModel)
        {
            string        captchaFormat        = GenerateCaptchaImage(buildInfoModel);
            string        generateTokenElement = GenerateTokenElement(buildInfoModel);
            string        inputElement         = GenerateInputElement(buildInfoModel);
            RefreshButton refreshButton        = GenerateRefreshButton(buildInfoModel);
            string        markup = string.Format("{0}{1} <br/>{2}<br/>{3}<br/>{4}", captchaFormat,
                                                 generateTokenElement, refreshButton.Markup,
                                                 buildInfoModel.InputText, inputElement);

            return(new CaptchaModel(buildInfoModel, markup, refreshButton.Script));
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="PartialBuildInfoModel"/> class.
 /// </summary>
 public PartialBuildInfoModel(HtmlHelper htmlHelper, IBuildInfoModel buildInfoModel, string partialViewName, string scriptPartialViewName,
                              ViewDataDictionary viewData)
 {
     Validate.ArgumentNotNull(htmlHelper, "htmlHelper");
     Validate.ArgumentNotNull(buildInfoModel, "buildInfoModel");
     Validate.ArgumentNotNullOrEmpty(partialViewName, "partialViewName");
     HtmlHelper            = htmlHelper;
     BuildInfoModel        = buildInfoModel;
     PartialViewName       = partialViewName;
     ScriptPartialViewName = scriptPartialViewName;
     ViewData = viewData;
 }
        /// <summary>
        ///     Creates a new captcha using the specified <see cref="IBuildInfoModel" />.
        /// </summary>
        /// <param name="buildInfoModel">
        ///     The specified <see cref="IBuildInfoModel" />.
        /// </param>
        /// <returns>An instance of <see cref="ICaptcha"/>.</returns>
        public virtual ICaptcha Build(IBuildInfoModel buildInfoModel)
        {
            RefreshButton captchaFormat        = GenerateCaptchaImage(buildInfoModel);
            string        generateTokenElement = GenerateTokenElement(buildInfoModel);
            string        inputElement         = GenerateInputElement(buildInfoModel);
            //  RefreshButton refreshButton = GenerateRefreshButton(buildInfoModel);
            //string markup = string.Format("{0}{1} <br/>{2}<br/>{3}<br/>{4}", captchaFormat,
            //                              generateTokenElement, refreshButton.Markup,
            //                              buildInfoModel.InputText, inputElement);
            string markup = string.Format("<div class=\"row\"><div class=\"col-md-6\">{4}</div><div class=\"col-md-6\">{0}{1}{2}{3}</div></div>", captchaFormat.Markup,
                                          generateTokenElement, "",
                                          buildInfoModel.InputText, inputElement);

            return(new CaptchaModel(buildInfoModel, markup, captchaFormat.Script));
        }
        /// <summary>
        ///     Creates a html string to represent the refresh button element.
        /// </summary>
        /// <param name="buildInfoModel">
        ///     The specified <see cref="IBuildInfoModel" />.
        /// </param>
        /// <returns>The html string with the refresh button element.</returns>
        protected virtual RefreshButton GenerateRefreshButton(IBuildInfoModel buildInfoModel)
        {
            string id           = Guid.NewGuid().ToString("N");
            string functionName = string.Format("______{0}________()", Guid.NewGuid().ToString("N"));
            var    tagA         = new TagBuilder("a");

            tagA.Attributes.Add("onclick", functionName);
            tagA.Attributes.Add("href", "#" + buildInfoModel.ImageElementId);
            tagA.Attributes.Add("style", "display:none;");
            tagA.SetInnerText(buildInfoModel.RefreshButtonText);
            tagA.Attributes.Add("id", id);
            string updateScript = string.Format(UpdateScript, id, buildInfoModel.RefreshUrl,
                                                buildInfoModel.TokenParameterName,
                                                buildInfoModel.TokenElementId, functionName);

            return(new RefreshButton(tagA.ToString(), updateScript));
        }
Example #9
0
 /// <summary>
 ///     Gets the <see cref="ICaptchaBulder" /> for build captcha with specified <see cref="IBuildInfoModel" />.
 /// </summary>
 /// <param name="buildInfoModel">
 ///     The specified <see cref="IBuildInfoModel" />.
 /// </param>
 /// <returns>
 ///     An instance of <see cref="ICaptchaBulder" />.
 /// </returns>
 private static ICaptchaBulder GetCaptchaBuilder(IBuildInfoModel buildInfoModel)
 {
     if (buildInfoModel is DefaultBuildInfoModel)
     {
         return(new DefaultCaptchaBuilder());
     }
     if (buildInfoModel is MathBuildInfoModel)
     {
         return(new MathCaptchaBuilder());
     }
     if (buildInfoModel is PartialBuildInfoModel)
     {
         return(new PartialCaptchaBuilder());
     }
     throw new NotSupportedException(
               "DefaultCaptchaBuilderProvider does not support the type of a IBuildInfoModel = " +
               buildInfoModel.GetType());
 }
Example #10
0
        /// <summary>
        ///     Creates a html string to represent the refresh button element.
        /// </summary>
        /// <param name="buildInfoModel">
        ///     The specified <see cref="IBuildInfoModel" />.
        /// </param>
        /// <returns>The html string with the refresh button element.</returns>
        protected override RefreshButton GenerateRefreshButton(IBuildInfoModel buildInfoModel)
        {
            var infoModel = buildInfoModel as MathBuildInfoModel;
            if (infoModel == null)
                throw new ArgumentException("A MathCaptchaBuilder can only work with the MathBuildInfoModel.");

            string id = Guid.NewGuid().ToString("N");
            string functionName = string.Format("______{0}________()", Guid.NewGuid().ToString("N"));
            var tagA = new TagBuilder("a");
            tagA.Attributes.Add("onclick", functionName);
            tagA.Attributes.Add("href", "#" + buildInfoModel.ImageElementId);
            tagA.Attributes.Add("style", "display:none;");
            tagA.SetInnerText(buildInfoModel.RefreshButtonText);
            tagA.Attributes.Add("id", id);
            string updateScript = string.Format(UpdateScript, id, infoModel.RefreshUrl, infoModel.TokenParameterName,
                                                infoModel.TokenElementId, infoModel.MathParamterName, functionName);
            return new RefreshButton(tagA.ToString(), updateScript);
        }
Example #11
0
        /// <summary>
        ///     Creates a new captcha using the specified <see cref="IBuildInfoModel" />.
        /// </summary>
        /// <param name="buildInfoModel">
        ///     The specified <see cref="IBuildInfoModel" />.
        /// </param>
        /// <returns>An instance of <see cref="ICaptcha"/>.</returns>
        public ICaptcha Build(IBuildInfoModel buildInfoModel)
        {
            var infoModel = buildInfoModel as PartialBuildInfoModel;

            if (infoModel == null)
            {
                throw new ArgumentException("A PartialCaptchaBuilder can only work with a PartialBuildInfoModel.");
            }

            MvcHtmlString markup;
            MvcHtmlString script = null;

            if (infoModel.ViewData != null)
            {
                markup = infoModel.HtmlHelper.Partial(infoModel.PartialViewName, infoModel.BuildInfoModel,
                                                      infoModel.ViewData);
                if (!string.IsNullOrEmpty(infoModel.ScriptPartialViewName))
                {
                    script = infoModel.HtmlHelper.Partial(infoModel.ScriptPartialViewName, infoModel.BuildInfoModel,
                                                          infoModel.ViewData);
                }
            }
            else
            {
                markup = infoModel.HtmlHelper.Partial(infoModel.PartialViewName, infoModel.BuildInfoModel);
                if (!string.IsNullOrEmpty(infoModel.ScriptPartialViewName))
                {
                    script = infoModel.HtmlHelper.Partial(infoModel.ScriptPartialViewName, infoModel.BuildInfoModel);
                }
            }
            if (script == null)
            {
                return(new CaptchaModel(buildInfoModel, markup.ToHtmlString(), null));
            }
            return(new CaptchaModel(buildInfoModel, markup.ToHtmlString(), script.ToHtmlString()));
        }
Example #12
0
 /// <summary>
 ///     Creates a html string to represent the token element.
 /// </summary>
 /// <param name="buildInfoModel">
 ///     The specified <see cref="IBuildInfoModel" />.
 /// </param>
 /// <returns>The html string with the token element.</returns>
 protected virtual string GenerateTokenElement(IBuildInfoModel buildInfoModel)
 {
     return(buildInfoModel.HtmlHelper.Hidden(buildInfoModel.TokenElementId,
                                             buildInfoModel.TokenValue).ToHtmlString());
 }
Example #13
0
 /// <summary>
 ///     Creates a new captcha to the specified <see cref="IBuildInfoModel" />.
 /// </summary>
 /// <param name="buildInfoModel">
 ///     The specified <see cref="IBuildInfoModel" />.
 /// </param>
 /// <returns>The html string with the captcha.</returns>
 public virtual ICaptcha GenerateCaptcha(IBuildInfoModel buildInfoModel)
 {
     Validate.ArgumentNotNull(buildInfoModel, "buildInfoModel");
     return(CaptchaBuilderFactory(buildInfoModel).Build(buildInfoModel));
 }
Example #14
0
 /// <summary>
 ///     Creates a html string to represent the image.
 /// </summary>
 /// <param name="buildInfoModel">
 ///     The specified <see cref="IBuildInfoModel" />.
 /// </param>
 /// <returns>The html string with the image.</returns>
 protected virtual string GenerateCaptchaImage(IBuildInfoModel buildInfoModel)
 {
     return(string.Format(CaptchaFormat, buildInfoModel.ImageElementId, buildInfoModel.ImageUrl));
 }