ASP.NET,aspxクリック認証コードを更新しない

8228 ワード

src="code.aspx"=src="code.aspx?id="+Math.random();
認証コードページ:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Drawing;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class code : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DrawCode();
    }
    public void DrawCode()
    {
        int width = 60;
        int height = 20;
        //    
        Bitmap img = new Bitmap(width, height);
        //           
        Graphics g = Graphics.FromImage(img);
        try
        {
            //    
            Font font = new Font("Comic sans ms", 12, FontStyle.Bold);
            //      
            SolidBrush brush = new SolidBrush(Color.Black);
            //    ,     
            Pen pen1 = new Pen(Color.Gray);
            Pen pen2 = new Pen(Color.Gray);

            //                
            g.Clear(ColorTranslator.FromHtml("#F0F0F0"));
            //           
            Rectangle rect = new Rectangle(2, 2, width, height);
            //              
            Random rand = new Random();
            //         
            for (int i = 0; i < 2; i++)
            {
                //Define Point1
                Point p1 = new Point(0, rand.Next(height));
                //Define Point2
                Point p2 = new Point(width, rand.Next(height));
                g.DrawLine(pen1, p1, p2);
            }
            //         
            for (int i = 0; i < 4; i++)
            {
                //Define Point1
                Point p1 = new Point(rand.Next(width), 0);
                //Define Point2
                Point p2 = new Point(rand.Next(width), height);
                //DrawLine
                g.DrawLine(pen2, p1, p2);
            }
            string strsj = strRand();
            g.DrawString(strsj, font, brush, rect);
            //                
            img.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
            Session["iCode"] = strsj.ToLower();
            //return strRand().ToLower();
        }
        catch (Exception error)
        {
            throw new Exception(error.Message);
        }
        finally
        {
            g.Dispose();
            img.Dispose();
        }
    }
    //     
    public string strRand()
    {
        char[] strCode = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
        string strRandomCode = "";
        Random random = new Random();
        for (int i = 0; i < 4; i++)
        {
            strRandomCode += strCode[random.Next(strCode.Length)];
        }
        return strRandomCode;
    }
}

呼び出し方法:
<label>   :</label>
                            <asp:TextBox ID="code" runat="server" style= "width:50px;"></asp:TextBox>
                            <img id="icode" src="code.aspx" alt="   " onclick="javascript:reloadcode(); " style="cursor:pointer;padding:2px 8px 0pt 3px;" /><!--           -->
                            <p/>
                            <a href="javascript:reloadcode()" >    </a>&nbsp;<br /><!--         -->
                            <p/>


<script language="JavaScript" type="text/javascript">
  function reloadcode() {
    var verify = document.getElementById('icode');
    verify.setAttribute('src', 'code.aspx?id='+Math.random());

  }