Forum list » General discussion »
MVC control for Captcha
Pages:  1
(11 posts)

MVC control for Captcha

Hi

Do you have a default CAPTHCA control that can be used from a razor view?

 

(11 posts)

I've solved it, by using the WAF.Presentation.Web.Captcha.GetCaptchaBitmap function and created a new Html helper for it.

The helper method:

public static MvcHtmlString CaptchaImage(this HtmlHelper helper, string captchaAnswer, int width, int height)
		{
			var captchaImage = WAF.Presentation.Web.Captcha.GetCaptchaBitmap(captchaAnswer, height, width, System.Drawing.Color.Gray, System.Drawing.Color.LightSlateGray, System.Drawing.Color.White, 35);
			var imageBase64 = Convert.ToBase64String(captchaImage);
			var imageSrc = string.Format("data:image/png;base64,{0}", imageBase64);
			var builder = new TagBuilder("img");
			builder.MergeAttribute("src", imageSrc);
			builder.MergeAttribute("alt", "Captcha image");
			return MvcHtmlString.Create(builder.ToString(TagRenderMode.SelfClosing));
		}

I set the captcha answer in the ViewBag and in the view I can use:

@Html.CaptchaImage((string)ViewBag.CaptchaAnswer, 150, 50)
 
(144 posts)

Great that you found a good solution!

As you noticed, most of the functionality in our web controls is exposed through our API, and can be easily used in MVC by creating custom Html Helpers.