captcha.php
I will show you how to create a ASP.NET Captcha control without using any HTTP HANDELRS or what so ever. you can easily plug it into your web pages.
我将向您展示如何在不使用任何HTTP HANDELRS的情况下创建ASP.NET Captcha控件。 您可以轻松地将其插入网页。
For Example
例如
a = 2 + 3 (where 2 and 3 are 2 random numbers)
a = 2 + 3(其中2和3是2个随机数)
Session("Answer") = 5
会话(“答案”)= 5
then we will create a image using System.drawing namespace with the text " 2+ 5 = " within it. We will store the result into a session variable, so later on our webpage we can use this session variable to compare with what user types.
然后我们将使用System.drawing命名空间创建一个图像,其中包含文本“ 2+ 5 =”。 我们会将结果存储到会话变量中,因此稍后在我们的网页上,我们可以使用该会话变量与用户类型进行比较。
Lets see the code of Captcha.aspx:
让我们看一下Captcha.aspx的代码:
Imports System.Drawing
Partial Class Captcha
Inherits System.Web.UI.Page
Private Sub returnNumer()
Dim num1 As New Random
Dim num2 As New Random
Dim numQ1 As Integer
Dim numQ2 As Integer
Dim QString As String
numQ1 = num1.Next(10, 15)
numQ2 = num1.Next(17, 31)
QString = numQ1.ToString + " + " + numQ2.ToString + " = "
Session("answer") = numQ1 + numQ2
Dim bitmap As New Bitmap(85, 35)
Dim Grfx As Graphics = Graphics.FromImage(bitmap)
Dim font As New Font("Arial", 18, FontStyle.Bold, GraphicsUnit.Pixel)
Dim Rect As New Rectangle(0, 0, 100, 50)
Grfx.FillRectangle(Brushes.Brown, Rect)
Grfx.DrawRectangle(Pens.PeachPuff, Rect) ' Border
Grfx.DrawString(QString, font, Brushes.Azure, 0, 0)
Response.ContentType = "Image/jpeg"
bitmap.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg)
bitmap.Dispose()
Grfx.Dispose()
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Call Me.returnNumer()
End Sub
End Class
So the Page_Load event generates an image.
因此,Page_Load事件生成一个图像。
Now we can use this Page within any aspx page for verification. For Example here is form2.aspx
现在,我们可以在任何aspx页面中使用此页面进行验证。 例如,这里是form2.aspx
<asp:image runat=server imgeurl="captcha.aspx">
<asp:image runat = server imgeurl =“ captcha.aspx”>
now you can create a TextBox, have users enter some text into it, and then create a ANSWER button, upon clicking of ANSWER button compare the value of textbox with the Session variable Session("answer"), if both are same then Verification passed.
现在,您可以创建一个TextBox,让用户在其中输入一些文本,然后创建一个ANSWER按钮,单击ANSWER按钮后,将文本框的值与会话变量Session(“ answer”)进行比较,如果两者相同,则验证通过。
Any questions please post below.
如有任何疑问,请在下面发布。
Thanks for reading.
谢谢阅读。
翻译自: https://www.experts-exchange.com/articles/9871/Simple-ASP-NET-CAPTCHA.html
captcha.php