1
Imports System.ComponentModel
2
Imports System.Web
3
Imports System.Web.UI
4
Imports System.Web.UI.WebControls
5
Imports System.Collections
6
Imports System.Collections.Specialized
7
8
/**/''' <summary>
9
''' CAPTCHA ASP.NET 2.0 user control
10
''' </summary>
11
''' <remarks>
12
''' add a reference to this DLL and add the CaptchaControl to your toolbox;
13
''' then just drag and drop the control on a web form and set properties on it.
14
''' 该源码下载自www.51aspx.com(51aspx.com)
15
''' Jeff Atwood
16
''' http://www.codinghorror.com/
17
''' </remarks>
18
<DefaultProperty("Text")> _
19
Public Class CaptchaControlClass CaptchaControl
20
Inherits System.Web.UI.WebControls.WebControl
21
Implements INamingContainer
22
Implements IPostBackDataHandler
23
Implements IValidator
24
25
Public Enum LayoutEnum Layout
26
Horizontal
27
Vertical
28
End Enum
29
30
Public Enum CacheTypeEnum CacheType
31
HttpRuntime
32
Session
33
End Enum
34
35
Private _timeoutSecondsMax As Integer = 90
36
Private _timeoutSecondsMin As Integer = 3
37
Private _userValidated As Boolean = True
38
Private _text As String = "Enter the code shown:"
39
Private _font As String = ""
40
Private _captcha As CaptchaImage = New CaptchaImage
41
Private _layoutStyle As Layout = Layout.Horizontal
42
Private _prevguid As String
43
Private _errorMessage As String = ""
44
Private _cacheStrategy As CacheType = CacheType.HttpRuntime
45
46
Public Properties#Region " Public Properties"
47
48
<Browsable(False), _
49
Bindable(True), _
50
Category("Appearance"), _
51
DefaultValue("The text you typed does not match the text in the image."), _
52
Description("Message to display in a Validation Summary when the CAPTCHA fails to validate.")> _
53
Public Property ErrorMessage()Property ErrorMessage() As String Implements System.Web.UI.IValidator.ErrorMessage
54
Get
55
If Not _userValidated Then
56
Return _errorMessage
57
Else
58
Return ""
59
End If
60
End Get
61
Set(ByVal value As String)
62
_errorMessage = value
63
End Set
64
End Property
65
66
<Browsable(False), _
67
Category("Behavior"), _
68
DefaultValue(True), _
69
Description("Is Valid"), _
70
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)> _
71
Public Property IsValid()Property IsValid() As Boolean Implements System.Web.UI.IValidator.IsValid
72
Get
73
Return _userValidated
74
End Get
75
Set(ByVal value As Boolean)
76
End Set
77
End Property
78
79
Public Overrides Property Enabled()Property Enabled() As Boolean
80
Get
81
Return MyBase.Enabled
82
End Get
83
Set(ByVal value As Boolean)
84
MyBase.Enabled = value
85
' When a validator is disabled, generally, the intent is not to
86
' make the page invalid for that round trip.
87
If Not value Then
88
_userValidated = True
89
End If
90
End Set
91
End Property
92
93
94
<DefaultValue("Enter the code shown above:"), _
95
Description("Instructional text displayed next to CAPTCHA image."), _
96
Category("Appearance")> _
97
Public Property [()Property [Text]() As String
98
Get
99
Return _text
100
End Get
101
Set(ByVal Value As String)
102
_text = Value
103
End Set
104
End Property
105
106
<DefaultValue(GetType(CaptchaControl.Layout), "Horizontal"), _
107
Description("Determines if image and input area are displayed horizontally, or vertically."), _
108
Category("Captcha")> _
109
Public Property LayoutStyle()Property LayoutStyle() As Layout
110
Get
111
Return _layoutStyle
112
End Get
113
Set(ByVal Value As Layout)
114
_layoutStyle = Value
115
End Set
116
End Property
117
118
<DefaultValue(GetType(CaptchaControl.CacheType), "HttpRuntime"), _
119
Description("Determines if CAPTCHA codes are stored in HttpRuntime (fast, but local to current server) or Session (more portable across web farms)."), _
120
Category("Captcha")> _
121
Public Property CacheStrategy()Property CacheStrategy() As CacheType
122
Get
123
Return _cacheStrategy
124
End Get
125
Set(ByVal value As CacheType)
126
_cacheStrategy = value
127
End Set
128
End Property
129
130
<Description("Returns True if the user was CAPTCHA validated after a postback."), _
131
Category("Captcha")> _
132
Public ReadOnly Property UserValidated()Property UserValidated() As Boolean
133
Get
134
Return _userValidated
135
End Get
136
End Property
137
138
139
<DefaultValue(""), _
140
Description("Font used to render CAPTCHA text. If font name is blank, a random font will be chosen."), _
141
Category("Captcha")> _
142
Public Property CaptchaFont()Property CaptchaFont() As String
143
Get
144
Return _font
145
End Get
146
Set(ByVal Value As String)
147
_font = Value
148
_captcha.Font = _font
149
End Set
150
End Property
151
152
<DefaultValue(""), _
153
Description("Characters used to render CAPTCHA text. A character will be picked randomly from the string."), _
154
Category("Captcha")> _
155
Public Property CaptchaChars()Property CaptchaChars() As String
156
Get
157
Return _captcha.TextChars
158
End Get
159
Set(ByVal Value As String)
160
_captcha.TextChars = Value
161
End Set
162
End Property
163
164
<DefaultValue(5), _
165
Description("Number of CaptchaChars used in the CAPTCHA text"), _
166
Category("Captcha")> _
167
Public Property CaptchaLength()Property CaptchaLength() As Integer
168
Get
169
Return _captcha.TextLength
170
End Get
171
Set(ByVal Value As Integer)
172
_captcha.TextLength = Value
173
End Set
174
End Property
175
176
<DefaultValue(2), _
177
Description("Minimum number of seconds CAPTCHA must be displayed before it is valid. If you're too fast, you must be a robot. Set to zero to disable."), _
178
Category("Captcha")> _
179
Public Property CaptchaMinTimeout()Property CaptchaMinTimeout() As Integer
180
Get
181
Return _timeoutSecondsMin
182
End Get
183
Set(ByVal Value As Integer)
184
If Value > 15 Then
185
Throw New ArgumentOutOfRangeException("CaptchaTimeout", "Timeout must be less than 15 seconds. Humans aren't that slow!")
186
End If
187
_timeoutSecondsMin = Value
188
End Set
189
End Property
190
191
<DefaultValue(90), _
192
Description("Maximum number of seconds CAPTCHA will be cached and valid. If you're too slow, you may be a CAPTCHA hack attempt. Set to zero to disable."), _
193
Category("Captcha")> _
194
Public Property CaptchaMaxTimeout()Property CaptchaMaxTimeout() As Integer
195
Get
196
Return _timeoutSecondsMax
197
End Get
198
Set(ByVal Value As Integer)
199
If Value < 15 And Value <> 0 Then
200
Throw New ArgumentOutOfRangeException("CaptchaTimeout", "Timeout must be greater than 15 seconds. Humans can't type that fast!")
201
End If
202
_timeoutSecondsMax = Value
203
End Set
204
End Property
205
206
<DefaultValue(50), _
207
Description("Height of generated CAPTCHA image."), _
208
Category("Captcha")> _
209
Public Property CaptchaHeight()Property CaptchaHeight() As Integer
210
Get
211
Return _captcha.Height
212
End Get
213
Set(ByVal Value As Integer)
214
_captcha.Height = Value
215
End Set
216
End Property
217
218
<DefaultValue(180), _
219
Description("Width of generated CAPTCHA image."), _
220
Category("Captcha")> _
221
Public Property CaptchaWidth()Property CaptchaWidth() As Integer
222
Get
223
Return _captcha.Width
224
End Get
225
Set(ByVal Value As Integer)
226
_captcha.Width = Value
227
End Set
228
End Property
229
230
<DefaultValue(GetType(CaptchaImage.FontWarpFactor), "Low"), _
231
Description("Amount of random font warping used on the CAPTCHA text"), _
232
Category("Captcha")> _
233
Public Property CaptchaFontWarping()Property CaptchaFontWarping() As CaptchaImage.FontWarpFactor
234
Get
235
Return _captcha.FontWarp
236
End Get
237
Set(ByVal Value As CaptchaImage.FontWarpFactor)
238
_captcha.FontWarp = Value
239
End Set
240
End Property
241
242
<DefaultValue(GetType(CaptchaImage.BackgroundNoiseLevel), "Low"), _
243
Description("Amount of background noise to generate in the CAPTCHA image"), _
244
Category("Captcha")> _
245
Public Property CaptchaBackgroundNoise()Property CaptchaBackgroundNoise() As CaptchaImage.BackgroundNoiseLevel
246
Get
247
Return _captcha.BackgroundNoise
248
End Get
249
Set(ByVal Value As CaptchaImage.BackgroundNoiseLevel)
250
_captcha.BackgroundNoise = Value
251
End Set
252
End Property
253
254
<DefaultValue(GetType(CaptchaImage.LineNoiseLevel), "None"), _
255
Description("Add line noise to the CAPTCHA image"), _
256
Category("Captcha")> _
257
Public Property CaptchaLineNoise()Property CaptchaLineNoise() As CaptchaImage.LineNoiseLevel
258
Get
259
Return _captcha.LineNoise
260
End Get
261
Set(ByVal Value As CaptchaImage.LineNoiseLevel)
262
_captcha.LineNoise = Value
263
End Set
264
End Property
265
#End Region
266
267
Public Sub Validate()Sub Validate() Implements System.Web.UI.IValidator.Validate
268
'-- a no-op, since we validate in LoadPostData
269
End Sub
270
271
Private Function GetCachedCaptcha()Function GetCachedCaptcha(ByVal guid As String) As CaptchaImage
272
If _cacheStrategy = CacheType.HttpRuntime Then
273
Return CType(HttpRuntime.Cache.Get(guid), CaptchaImage)
274
Else
275
Return CType(HttpContext.Current.Session.Item(guid), CaptchaImage)
276
End If
277
End Function
278
279
Private Sub RemoveCachedCaptcha()Sub RemoveCachedCaptcha(ByVal guid As String)
280
If _cacheStrategy = CacheType.HttpRuntime Then
281
HttpRuntime.Cache.Remove(guid)
282
Else
283
HttpContext.Current.Session.Remove(guid)
284
End If
285
End Sub
286
287
/**/''' <summary>
288
''' are we in design mode?
289
''' </summary>
290
Private ReadOnly Property IsDesignMode()Property IsDesignMode() As Boolean
291
Get
292
Return HttpContext.Current Is Nothing
293
End Get
294
End Property
295
296
/**/''' <summary>
297
''' Validate the user's text against the CAPTCHA text
298
''' </summary>
299
Private Sub ValidateCaptcha()Sub ValidateCaptcha(ByVal userEntry As String)
300
301
If Not Visible Or Not Enabled Then
302
_userValidated = True
303
Return
304
End If