在ASP页中插入CAPTCHA [英] Inserting CAPTCHA in the ASP Page

查看:57
本文介绍了在ASP页中插入CAPTCHA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我想在我的网站上为页面实施验证码.我正在创建一个矩形,并在其中插入随机字符串.下面是相同的代码.

Hello All,

I want to implement Captcha for a pagein my website. I am creating a rectangle and inserting a random string into it. Below is the code for the same.

int temp;
    Random GenRand = new Random();

    temp = GenRand.Next(1, 9);
    StringBuilder sb = new StringBuilder();
    sb.Append(temp);
    Random random = new Random();
    char ch;
    for (int i = 0; i < 4; i++)
    {
        ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
        sb.Append(ch);
    }
    string captcha = sb.Append(temp).ToString();
    
    
    Bitmap b = new Bitmap(1000,1000);
   
    Graphics objGraphic = Graphics.FromImage(b);
    
    SolidBrush s = new SolidBrush(Color.Black);
    SolidBrush s1=new SolidBrush(Color.White);


    Font font=new Font (SystemFonts.DefaultFont.FontFamily,20);
    Rectangle r = new Rectangle(0,0,950,50);
    
    objGraphic.DrawString(captcha, font, s1, r);

    
   
	
        Response.ContentType = "image/gif";
 
        b.Save (Response.OutputStream, ImageFormat.Gif);



如何在我的网页中使用它?

如果我在.aspx页的脚本中使用PageLoad事件,则不会显示普通页,而仅显示上述页的输出.

有人可以帮忙吗?

非常感谢Advance



How to use this in my web page ?

If i use the PageLoad event in the script in the .aspx page, the normal page wont be displayed, and only the output of the above page is displayed.

Can anybody assist ??

Many Thanks in Advance

推荐答案

[ ^ ]是以下内容之一CP上的Captcha文章数量-您可以细读这些文章以了解事物的工作方式,或者只是抓住自己喜欢的外观并按原样使用它.
This[^] is one of a number of Captcha articles on CP - you can peruse those to see how things should work or just grab one that you like the look of and use it as is.


你好,

我对您的代码进行了一些更改,可以说此代码位于 TestCaptcha.aspx 代码后面的

hello,

I make some changes to your code, let say this code is in TestCaptcha.aspx code behind

int temp;
        Random GenRand = new Random();

        temp = GenRand.Next(1, 9);
        StringBuilder sb = new StringBuilder();
        sb.Append(temp);
        Random random = new Random();
        char ch;
        for (int i = 0; i < 4; i++)
        {
            ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
            sb.Append(ch);
        }
        string captcha = sb.Append(temp).ToString();


        Bitmap b = new Bitmap(120, 40);

        Graphics objGraphic = Graphics.FromImage(b);

        SolidBrush s = new SolidBrush(Color.Black);
        SolidBrush s1 = new SolidBrush(Color.White);


        Font font = new Font(SystemFonts.DefaultFont.FontFamily, 20);
        Rectangle r = new Rectangle(0, 0, 950, 50);

        Session["CaptchaString"] = captcha;

        objGraphic.DrawString(captcha, font, s1, r);
        System.IO.MemoryStream ms = new System.IO.MemoryStream();
        b.Save(ms, ImageFormat.Gif);
        Response.ClearContent();
        Response.ContentType = "image/gif";
        Response.BinaryWrite(ms.ToArray());

        objGraphic.Dispose();
        b.Dispose();



让我们创建另一个页面调用 TestCallCaptcha.aspx 进行测试.

将以下代码添加到 TestCallCaptcha.aspx 页.请注意,图像 src 属性指向 TestCaptcha.aspx 页面




Let create another page call TestCallCaptcha.aspx for testing.

Add the below code to the TestCallCaptcha.aspx page. Notice that the image src attribute is pointing to TestCaptcha.aspx page


<div>
   <img style="width: 82px; height: 23px;" alt="captcha"
       src="TestCaptcha.aspx" />
       <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
       <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
   </div>




然后将以下代码添加到 TestCallCaptcha.aspx.cs





Then add the below code to TestCallCaptcha.aspx.cs


protected void Button1_Click(object sender, EventArgs e)
    {
        if (TextBox1.Text != Session["CaptchaString"] as string)
        {
            Response.Write("Not match");
        }
        else
        {
            Response.Write("Match !!!");
        }
    }




希望对您有所帮助.

谢谢,
布莱恩·谭(Bryian Tan)




Hope it help.

Thanks,
Bryian Tan


这篇关于在ASP页中插入CAPTCHA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆