One of my forms has a captcha.
I am forced to use this captcha since there’s a lot of code already written before me.
This way is simple:
There is an ashx file. An image will be created using that file. At the same time, that file will create a session with the same value from the image.
Upon submission, the code will check whether they are the same. If so, continue. if not, return.
Upon page refresh, the captcha will update.
Then I need to add in ajax update panel as the requirement.
Then the captcha is still working fine in chrome and safari but it is not refreshing when the page loads again in IE and firefox.
I created a simple page just for captcha
aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default7.aspx.cs" Inherits="Default7" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<img height="30" alt="" src="Handler.ashx" width="80"><br>
<asp:Button ID="btnSubmit" runat="server" Text="Submit"
onclick="btnSubmit_Click"/>
</ContentTemplate>
</asp:UpdatePanel>
<div>
</div>
</form>
</body>
</html>
aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Default7 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
}
}
handler.ashx
<%@ WebHandler Language="C#" Class="Handler" %>
using System;
using System.Web;
using System.Web.SessionState;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Text;
public class Handler : IHttpHandler,System.Web.SessionState.IRequiresSessionState {
public void ProcessRequest(HttpContext context)
{
Bitmap objBMP = new System.Drawing.Bitmap(60, 20);
Graphics objGraphics = System.Drawing.Graphics.FromImage(objBMP);
//objGraphics.Clear(Color.Blue);
objGraphics.TextRenderingHint = TextRenderingHint.AntiAlias;
//' Configure font to use for text
Font objFont = new Font("Arial", 8, FontStyle.Bold);
string randomStr = "";
int[] myIntArray = new int[5];
int x;
//That is to create the random # and add it to our string
Random autoRand = new Random();
for (x = 0; x < 5; x++)
{
myIntArray[x] = System.Convert.ToInt32(autoRand.Next(0, 9));
randomStr += (myIntArray[x].ToString());
}
randomStr = GetRandomString();
//This is to add the string to session cookie, to be compared later
context.Session.Add("randomStr", randomStr);
//' Write out the text
objGraphics.DrawString(randomStr, objFont, Brushes.White, 3, 3);
//' Set the content type and return the image
context.Response.ContentType = "image/GIF";
objBMP.Save(context.Response.OutputStream, ImageFormat.Gif);
objFont.Dispose();
objGraphics.Dispose();
objBMP.Dispose();
}
public bool IsReusable
{
get
{
return false;
}
}
private string GetRandomString()
{
string[] arrStr = "A,B,C,D,1,2,3,4,5,6,7,8,9,0".Split(",".ToCharArray());
string strDraw = string.Empty;
Random r = new Random();
for (int i = 0; i < 5; i++)
{
strDraw += arrStr[r.Next(0, arrStr.Length - 1)];
}
return strDraw;
}
}
Any idea?
I got the answer now.
change image control to server control.
and in code behind change the image source with current date time
<img height="30" alt="" src="/Handler.ashx" width="80" runat="server" id="imgCaptcha">
imgCaptcha.Src = "Handler.ashx?dt=" + DateTime.Now.ToString();
change image control to server control.
and in code behind change the image source with current date time