noBot

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
    <asp:TextBox ID="tbSomething" runat="server"></asp:TextBox>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" />
<asp:Label ID="lbResult" runat="server"></asp:Label>
<%--接下来是NoBot控件的声明:--%>
        <cc1:NoBot ID="NoBot1" CutoffWindowSeconds="10" CutoffMaximumInstances="2"
  ResponseMinimumDelaySeconds="2"
  OnGenerateChallengeAndResponse="noBot_GenerateChallengeAndResponse"  runat="server" />


    </div>
    </form>
</body>
</html>

 

====CS=====

 protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                AjaxControlToolkit.NoBotState state;
                if ( this.NoBot1.IsValid(out state))
                {
                    lbResult.Text = "您的信息已经被提交!";
                }
                else
                {
                    string errorMessage = string.Empty;
                    switch (state)
                    {
                        case AjaxControlToolkit.NoBotState.InvalidAddressTooActive:
                            errorMessage = "该IP地址在短时间内提交了过多的请求。";
                            break;
                        case AjaxControlToolkit.NoBotState.InvalidBadResponse:
                            errorMessage = "浏览器中检测脚本未被运行或运行结果不正确。";
                            break;
                        case AjaxControlToolkit.NoBotState.InvalidBadSession:
                            errorMessage = "ASP.NET会话状态不可用。";
                            break;
                        case AjaxControlToolkit.NoBotState.InvalidResponseTooSoon:
                            errorMessage = "两次回送时间间隔过短。";
                            break;
                        case AjaxControlToolkit.NoBotState.InvalidUnknown:
                            errorMessage = "未知错误。";
                            break;
                    }
                    lbResult.Text = string.Format("请求被拒绝,原因:{0}", errorMessage);
                }
            }

        }

        protected void noBot_GenerateChallengeAndResponse(object sender, AjaxControlToolkit.NoBotEventArgs e)
        {
            Panel noBotPanel = new Panel();
            //接下来,生成两个随机数,将分别设置到该Panel的长和宽属性上:

            Random rand = new Random();
            int width = rand.Next(80);
            int height = rand.Next(120);
            //随后,为这个Panel指定一个随机的ID,指定ID是为了让之后的JavaScript中可以在客户端取到其生成的<div>,而选用随机的ID是为了让程序更加具有不确定性,进一步迷惑机器人程序:

            noBotPanel.ID = string.Format("noBotPanel{0}", rand.Next(1000));
            //然后将上面生成的长、宽应用到该Panel上:

            noBotPanel.Width = width;
            noBotPanel.Height = height;
            //为了不干扰页面的现有布局,我们还要设置一下该Panel的样式,将其隐藏起来:

            noBotPanel.Style.Add(HtmlTextWriterStyle.Visibility, "hidden");
            noBotPanel.Style.Add(HtmlTextWriterStyle.Position, "absolute");
            //注意第一句实际上是设置了visibility: hidden;,而并没有选择我们常用的display: none;。因为若选用后者,则浏览器将认为其大小为0。

            //然后将该Panel添加为NoBot的子控件,同样是为了避免可能出现的对页面结构的影响:
            NoBot1.Controls.Add(noBotPanel);
           // (sender as noBot).Controls.Add(noBotPanel);
            //然后设置将在浏览器中执行的这一段检验的JavaScript:

            e.ChallengeScript = string.Format("var noBotPanel = document.getElementById('{0}'); noBotPanel.offsetWidth * noBotPanel.offsetHeight;", noBotPanel.ClientID);
            //注意到这段JavaScript在运行时将首先通过该Panel的客户端ID得到其实际<div>元素的引用,然后使用offsetWidth和offsetHeight得到其实际大小,并将其乘积返回。这段JavaScript赋值给了e.ChallengeScript,即NoBotEventArgs类型对象的ChallengeScript属性上。

            //最后设置上面这段JavaScript的预期运行结果,非常简单:

            e.RequiredResponse = (width * height).ToString();
        }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值