0

我想如何在一个会话中生成一个彼此不同的验证码?

例如:在用户可以继续下一步之前,我有一个带有验证码的申请表。问题是,如果我在新的窗口选项卡中打开另一个相同的申请表,它将生成与第一个申请表相同的验证码。我需要的是不同的验证码值。

请帮我解决这个问题...tq

我正在使用 asp.net。

这里是示例代码

生成代码

private string GenerateRandomCode()
{
    string s = "";
    for (int i = 0; i < 6; i++)
        s = String.Concat(s, random.Next(10).ToString());
    return s;
}

生成验证码图像

namespace OneStepContactMe.Layouts.OneStepContactMe
{
    public partial class captchapage : UnsecuredLayoutsPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["Map"] == null)
                { 
                    Session["Map"] = GenerateRandomCode();      
                }
                // Create a CAPTCHA image using the text stored in the Session object.
                CaptchaImage ci = new CaptchaImage(this.Session["Map"].ToString(), 200, 50, "Century Schoolbook");

                // Change the response headers to output a JPEG image.
                this.Response.Clear();
                this.Response.ContentType = "image/jpeg";

                // Write the image to the response stream in JPEG format.
                ci.Image.Save(this.Response.OutputStream, ImageFormat.Jpeg);

                // Dispose of the CAPTCHA image object.
                ci.Dispose(); 
            }
            else {
            }
        }

        /// <summary>
        /// generate random 6 digit
        /// </summary>
        /// <returns> string of 6 digits </returns>
        private string GenerateRandomCode()
        {
            Random random = new Random();
            string s = "";
            for (int i = 0; i < 6; i++)
                s = String.Concat(s, random.Next(10).ToString());
            return s;
        }

        //to allow anonymous access to this pages
        protected override bool AllowAnonymousAccess
        {
            get
            {
                return true;
            }
        }

        protected override void OnPreInit(EventArgs e)
        {
            base.OnPreInit(e);
            this.MasterPageFile = "/_catalogs/masterpage/MyCorridor.MemberArea.v1.master";
        }
    }
4

1 回答 1

0

将随机局部变量更改为成员字段。例如

public partial class captchapage : UnsecuredLayoutsPageBase
{
    private Random random = new Random();
}
于 2012-10-17T07:44:16.007 回答