AJAX Control Toolkit - NoBot

关于Nobot可以参看NoBot,一下提供一个微软的案例:

 1 <form>   
2 <asp:ScriptManager ID="ScriptManager1" runat="server" />
3 <div>
4 <asp:MultiView ID="MultiView1" runat="server">
5 <asp:View ID="View1" runat="server">
6 <p>Please fill out the form below and submit it:</p>
7 <p>
8 First Name: <asp:TextBox ID="TextBox1" runat="server" Text="Anonymous" /><br />
9 Last Name: <asp:TextBox ID="TextBox2" runat="server" Text="User" />
10 </p>
11 <asp:Button ID="Button1" runat="server" Text="Submit" />
12 </asp:View>
13 <asp:View ID="View2" runat="server">
14 <p><b><asp:Label ID="Label1" runat="server" /></b></p>
15 <br />
16 <p>Explanation of possible responses:</p>
17 <ul>
18 <li><b>Valid</b>: All NoBot tests passed; user appears to be human</li>
19 <li><b>InvalidBadResponse</b>: An invalid response was provided to the challenge suggesting the challenge script was not run</li>
20 <li><b>InvalidResponseTooSoon</b>: The postback occurred quickly enough that a human was probably not involved</li>
21 <li><b>InvalidAddressTooActive</b>: The source IP address has submitted so many responses that a human was probably not involved</li>
22 <li><b>InvalidBadSession</b>: The ASP.NET session state for this session was unusable</li>
23 <li><b>InvalidUnknown</b>: An unknown problem occurred</li>
24 </ul>
25 <br />
26 <asp:HyperLink ID="HyperLink" runat="server" Text="Try again" NavigateUrl="~/Default.aspx" />
27 <br /><br />
28 <div style="font-size:8pt; background-color:#eeeeee; border-style:dashed; border-width:1pt; padding:3px">
29 <p>NoBot's user address cache (time IP):</p>
30 <p><asp:Label ID="Label2" runat="server" /></p>
31 </div>
32 </asp:View>
33 </asp:MultiView>
34
35 <ajaxToolkit:NoBot ID="NoBot1" runat="server" OnGenerateChallengeAndResponse="CustomChallengeResponse" />
36 </div>
37 </form>

  

 1  protected void Page_Load(object sender, EventArgs e)
2 {
3 if (IsPostBack)
4 {
5 // Display results view
6 MultiView1.SetActiveView(View2);
7 NoBotState state;
8 Label1.Text = string.Format(
9 (NoBot1.IsValid(out state) ?
10 "Congratulations, \"{1} {2}\", you do not appear to be a bot. (Details: {0})" :
11 "Rejected; user appears to be a bot. (Details: {0})"),
12 state.ToString(), TextBox1.Text, TextBox2.Text);
13 StringBuilder sb = new StringBuilder();
14 foreach (KeyValuePair<DateTime, string> kvp in NoBot.GetCopyOfUserAddressCache())
15 {
16 sb.AppendFormat("{0}: {1}<br />", kvp.Key.ToString("u"), kvp.Value);
17 }
18 Label2.Text = sb.ToString();
19 if (NoBot1.IsValid(out state))
20 {
21 Label1.BackColor = System.Drawing.Color.White;
22 }
23 else
24 {
25 Label1.BackColor = System.Drawing.Color.Red;
26 }
27 }
28 else
29 {
30 // Display input view
31 MultiView1.SetActiveView(View1);
32 }
33 }
34
35 protected void CustomChallengeResponse(object sender, NoBotEventArgs e)
36 {
37 // This is a sample challenge/response implementation that involves
38 // the DOM so as to make the calculation more difficult to thwart.
39 // It adds a randomly sized Panel; the client must calculate the area.
40 Panel p = new Panel();
41 p.ID = "NoBotSamplePanel";
42 Random rand = new Random();
43 p.Width = rand.Next(300);
44 p.Height = rand.Next(200);
45 p.Style.Add(HtmlTextWriterStyle.Visibility, "hidden");
46 p.Style.Add(HtmlTextWriterStyle.Position, "absolute");
47 ((NoBot)sender).Controls.Add(p);
48 e.ChallengeScript = string.Format("var e = document.getElementById('{0}'); e.offsetWidth * e.offsetHeight;", p.ClientID);
49 e.RequiredResponse = (p.Width.Value * p.Height.Value).ToString();
50 }

  

posted @ 2011-08-11 10:22  January  阅读(425)  评论(0编辑  收藏  举报