Developer Forum »
MVC control for Captcha
14 posts

Hi

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

 

14 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)
 
181 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.

1