2012-08-02 3 views
1

내 양식 중 하나에 보안 문자가 있습니다.보안 문자의 일관성없는 동작

나는 이미이 코드가 많이 있기 때문에이 captcha를 사용해야합니다.

이 방법은 간단하다 :

ashx 파일이 있습니다. 해당 파일을 사용하여 이미지가 생성됩니다. 동시에이 파일은 이미지의 동일한 값을 가진 세션을 만듭니다.

코드를 제출하면 코드가 동일한지 확인합니다. 그렇다면 계속하십시오. 그렇지 않다면 돌아 오십시오.

페이지 새로 고침시 보안 문자가 업데이트됩니다.

그런 다음 ajax update panel을 추가해야합니다.

그렇다면 크롬과 사파리에서 captcha는 계속 정상적으로 작동하지만 IE와 Firefox에서 페이지가 다시로드되면 새로 고침되지 않습니다.

난 그냥
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; 
     } 


} 

어떤 생각 보안 문자

에 대한 간단한 페이지를 만들어?


나는 지금 대답을 얻었다.

이미지 컨트롤을 서버 컨트롤로 변경.

및 코드에서

뒤에 현재 날짜 시간 서버 컨트롤

<img height="30" alt="" src="/Handler.ashx" width="80" runat="server" id="imgCaptcha">

imgCaptcha.Src = "Handler.ashx?dt=" + DateTime.Now.ToString();

답변

0

변경 이미지 컨트롤을 이미지 소스를 변경합니다.

및 코드에서

뒤에 현재 날짜 시간이 이미지 소스를 변경

<img height="30" alt="" src="/Handler.ashx" width="80" runat="server" id="imgCaptcha"> 

imgCaptcha.Src = "Handler.ashx?dt=" + DateTime.Now.ToString(); 
관련 문제