Using Ajax, How to display Captcha & authenticate it in Asp.net Web app, if the data(base64 & text hash) is coming as Json from
I made a Captcha class library named 'Utility.cs' that has a function called 'generateCaptcha()' that creates a random 6 digits text, creates a hash-code of it and then creates a image(with random impurities) based on the text. It then converts the image into base64 to string, and the base64 image data and hash are converted to Json (Serialized) and are returned in string to the function. The problem here is I am saving the captcha 'hash' and the 'image' in the session but I am not able to compare the captcha with the inputted text from TextBox which when matches will give an output as 'The captcha matches' since the Captcha never matches!. And once I click on submit the captcha refreshes(I have kept the generateCaptcha() function in page load, since I don't know where to generate the captcha other than page load). I was told that using Ajax can solve this problem. But I have only a little knowledge about Ajax and don't know how the Ajax code can fit in my program. The Json data is like this in the returned string 'str' : The code behind in '.aspx.cs' is this : public partial class About : Page { public static byte[] hash1; public static byte[] hash2; protected void Page_Load(object sender, EventArgs e) { utility u = new utility(); var json = u.generateCaptcha(); Captcha cap = JsonConvert.DeserializeObject<Captcha>(json); Session["Captcha_Image"] = cap; if (Session["Captcha_Image"] != null) { var captcha = (Captcha)Session["Captcha_Image"]; Label1.Text = GetImage(captcha.base64); } } public void Button1_Click(object sender, EventArgs e) { if(Session["Captcha_Image"] !=null) { var captcha =(Captcha)Session["Captcha_Image"]; hash1 = captcha.hash; using (MD5 md5 = MD5.Create()) { if (TextBox1.Text != "") { hash2 = md5.ComputeHash(Encoding.UTF8.GetBytes(TextBox1.Text));