Une de mes formes a un captcha.Comportement incohérent pour captcha
Je suis obligé d'utiliser ce captcha car il y a beaucoup de code écrit avant moi.
De cette façon est simple:
Il y a un fichier ashx
. Une image sera créée en utilisant ce fichier. En même temps, ce fichier crée une session avec la même valeur à partir de l'image.
Lors de la soumission, le code vérifiera s'ils sont identiques. Si oui, continuez. sinon, retour.
Lors de l'actualisation de la page, le captcha sera mis à jour.
Ensuite, j'ai besoin d'ajouter ajax update panel
comme exigence.
Ensuite, le captcha fonctionne toujours bien en chrome et safari mais il n'est pas rafraîchissant lorsque la page se charge à nouveau dans IE et Firefox.
J'ai créé une simple page juste pour 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;
}
}
Toute idée?
J'ai obtenu la réponse maintenant.
Changez le contrôle de l'image en contrôle serveur.
et dans le code change derrière la source d'image avec date et heure actuelle
<img height="30" alt="" src="/Handler.ashx" width="80" runat="server" id="imgCaptcha">
imgCaptcha.Src = "Handler.ashx?dt=" + DateTime.Now.ToString();