I'm having a captcha image generated from a page captcha.aspx
. It stores the "result" in a Session variable Session.Add("randomString", result);
. In my web form I am using a CompareValidator to compare the value entered by the user with the session value. The image displays correctly, however, on page load the Session variable seems to be null. What may be the problem?
Captcha.aspx.cs
//Removed the preceding code since it just adds the string
//This is to add the string to session cookie, to be compared later
Session.Add("randomStr", result);
//Write out the text
objGraphics.DrawString(expression, objFont, Brushes.White, 3, 3);
//Set the content type and return the image
Response.ContentType = "image/GIF";
objBMP.Save(Response.OutputStream, ImageFormat.Gif);
objFont.Dispose();
objGraphics.Dispose();
objBMP.Dispose();
Registration.aspx.cs
string captcha = Session["randomStr"] as String;
captchaC.ValueToCompare = captcha;
Registration.aspx
<asp:CompareValidator ID="captchaC" runat="server" ControlToValidate="txtCaptcha"
ErrorMessage="Invalid captcha" Display="None" Type="String"></asp:CompareValidator>