<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Dot Net Thoughts &#187; Captcha</title>
	<atom:link href="http://www.dotnetthoughts.net/tag/captcha/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dotnetthoughts.net</link>
	<description>thoughts about .Net, WPF, Sharepoint, Javascript and more.</description>
	<lastBuildDate>Wed, 28 Jul 2010 09:59:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Creating Captcha HTML Helper</title>
		<link>http://www.dotnetthoughts.net/2009/09/15/creating-captcha-html-helper/</link>
		<comments>http://www.dotnetthoughts.net/2009/09/15/creating-captcha-html-helper/#comments</comments>
		<pubDate>Tue, 15 Sep 2009 09:49:13 +0000</pubDate>
		<dc:creator>Anuraj P</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[.Net 3.0 / 3.5]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[ASP.Net MVC]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[C#.Net]]></category>
		<category><![CDATA[Captcha]]></category>
		<category><![CDATA[Captcha HTML Helper]]></category>
		<category><![CDATA[Custom HTML Helper]]></category>

		<guid isPermaLink="false">http://anuraj.wordpress.com/?p=389</guid>
		<description><![CDATA[After working with ASP.Net MVC, and my previous posts, in this Post I am trying to implement a Captcha HTML Helper, an HTML Helper is just a method that returns a string. Creating Custom HTML Helpers. It is an extension method, to the existing HTML Helper class. Here is code.(For more details about the Captcha [...]]]></description>
			<content:encoded><![CDATA[<p>After working with ASP.Net MVC, and my previous posts, in this Post I am trying to implement a Captcha HTML Helper, an HTML Helper is just a method that returns a string. <a href="http://www.asp.net/learn/mvc/tutorial-09-cs.aspx" target="_blank">Creating Custom HTML Helpers</a>. It is an extension method, to the existing HTML Helper class.<br />
Here is code.(For more details about the Captcha generation code, check my previous post. : <a href="http://anuraj.wordpress.com/2009/09/15/captcha-using-asp-net-and-c/">Captcha using ASP.Net and C#</a>);</p>
<pre class="brush: csharp;">
 public static class CaptchaHelper
    {
        public static string Captcha(this HtmlHelper helper, string text)
        {
            string srcPath = System.Web.VirtualPathUtility.ToAbsolute(&quot;~/Handler1.ashx&quot;);
            string htmlContent = string.Empty;
            htmlContent += &quot;&lt;script type=\&quot;text/javascript\&quot;&gt;function __rc(){document.getElementById(\&quot;&quot; + text +
                           &quot;\&quot;).src = \&quot;../Handler1.ashx?query=\&quot; + Math.random();}&lt;/script&gt;&quot;;
            htmlContent += string.Format(&quot;&lt;img id=\&quot;{0}\&quot; src=\&quot;{1}\&quot; alt=\&quot;Captcha Image\&quot;/&gt;&quot;, text, srcPath);
            htmlContent += &quot;&lt;a href=\&quot;#\&quot; onclick=\&quot;javascript:__rc();\&quot;&gt;Reset&lt;/a&gt;&quot;;
            return htmlContent;
        }
    }
</pre>
<p>And in the View you can use like, you may need to import the Namespace of CaptchaHelper class.</p>
<pre class="brush: xml;">
 &lt; %= Html.Captcha(&quot;Sample&quot;) %&gt;
</pre>
<p>It will render a security image. Happy Programming</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dotnetthoughts.net/2009/09/15/creating-captcha-html-helper/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Captcha using ASP.Net and C#</title>
		<link>http://www.dotnetthoughts.net/2009/09/15/captcha-using-asp-net-and-c/</link>
		<comments>http://www.dotnetthoughts.net/2009/09/15/captcha-using-asp-net-and-c/#comments</comments>
		<pubDate>Tue, 15 Sep 2009 06:49:59 +0000</pubDate>
		<dc:creator>Anuraj P</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[.Net 3.0 / 3.5]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[ASP.Net Handler]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[C#.Net]]></category>
		<category><![CDATA[Captcha]]></category>
		<category><![CDATA[Drawing]]></category>

		<guid isPermaLink="false">http://anuraj.wordpress.com/?p=380</guid>
		<description><![CDATA[Few days back, I got some question related to Captcha (security mechanism, which helps web masters to avoid spam) in a Forum. So I thought of implementing one. I got few nice scripts in Code Project, its a simple implementation, no too much logic and not too complex to understand. Also I am using an [...]]]></description>
			<content:encoded><![CDATA[<p>Few days back, I got some question related to Captcha (security mechanism, which helps web masters to avoid spam) in a Forum. So I thought of implementing one. I got few nice scripts in Code Project, its a simple implementation, no too much logic and not too complex to understand. Also I am using an HTTP Handler instead of ASPX Page, for the implementation.</p>
<pre class="brush: csharp;">
using (Bitmap b = new Bitmap(150, 40, PixelFormat.Format32bppArgb))
        {
            using (Graphics g = Graphics.FromImage(b))
            {
                Rectangle rect = new Rectangle(0, 0, 149, 39);
                g.FillRectangle(Brushes.White, rect);

                // Create string to draw.
                Random r = new Random();
                int startIndex = r.Next(1, 5);
                int length = r.Next(5, 10);
                String drawString = Guid.NewGuid().ToString().Replace(&quot;-&quot;, &quot;0&quot;).Substring(startIndex, length);

                // Create font and brush.
                Font drawFont = new Font(&quot;Arial&quot;, 16, FontStyle.Italic | FontStyle.Strikeout);
                using (SolidBrush drawBrush = new SolidBrush(Color.Black))
                {
                    // Create point for upper-left corner of drawing.
                    PointF drawPoint = new PointF(15, 10);

                    // Draw string to screen.
                    g.DrawRectangle(new Pen(Color.Red, 0), rect);
                    g.DrawString(drawString, drawFont, drawBrush, drawPoint);
                }
                b.Save(context.Response.OutputStream, ImageFormat.Jpeg);
                context.Response.ContentType = &quot;image/jpeg&quot;;
                context.Response.End();
            }
        }
</pre>
<p>I wrote the code in the Process Request event in HTTPHandler.<br />
And to use this in your pages you can create an IMG tag with src attribute pointing to this.</p>
<pre class="brush: xml;">
&lt;img src=&quot;myhandler.ashx&quot; /&gt;
</pre>
<p>Note: Some time ASP.Net caches the image, so you may need to pass some GUID as querystring in the Handler.</p>
<p>Update: Sometime we may need to refresh the Captcha image, without post back, here is a simple javascript which will refresh the captcha image without post back.</p>
<pre class="brush: xml;">
&lt;script type=&quot;text/javascript&quot;&gt;
        function RefreshCaptcha() {
            var img = document.getElementById(&quot;imgCaptcha&quot;);
            img.src = &quot;Handler.ashx?query=&quot; + Math.random();
        }
&lt;/script&gt;
&lt;div&gt;
&lt;img src=&quot;Handler.ashx&quot; id=&quot;imgCaptcha&quot; /&gt;
&lt;a href=&quot;#&quot; onclick=&quot;javascript:RefreshCaptcha();&quot;&gt;Refresh&lt;/a&gt;
&lt;/div&gt;
</pre>
<p>Here is the screenshot of web page using Captcha.</p>
<div id="attachment_387" class="wp-caption alignnone" style="width: 356px"><a href="http://anuraj.files.wordpress.com/2009/09/captcha.jpg"><img src="http://anuraj.files.wordpress.com/2009/09/captcha.jpg" alt="Captcha - Security Image using ASP.Net and C#" title="Captcha - Security Image using ASP.Net and C#" width="346" height="174" class="size-full wp-image-387" /></a><p class="wp-caption-text">Captcha - Security Image using ASP.Net and C#</p></div>
]]></content:encoded>
			<wfw:commentRss>http://www.dotnetthoughts.net/2009/09/15/captcha-using-asp-net-and-c/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
