3

我想向 CreateUserWizard 添加一个“反机器人”问题,作为验证码控件的更易于访问的替代方案。我对asp还很陌生,发现我有点陷入了WinForms的思维定势。但是,我想出了一些似乎可行的方法。

标记:

    <asp:CreateUserWizard ID="CreateUserWizard1" runat="server">
    .
    .
    <tr>
      <td align="right">
        <asp:Label ID="AntiRobotQuestion" runat="server" AssociatedControlID="AntiRobotAnswer">
          Question:
        </asp:Label>
      </td>
      <td>
        <asp:TextBox ID="AntiRobotAnswer" runat="server"></asp:TextBox>
        <asp:RequiredFieldValidator ID="AntiRobotAnswerRequired" runat="server" ControlToValidate="AntiRobotAnswer" ErrorMessage="Answer is required." ToolTip="Answer is required." ValidationGroup="CreateUserWizard1">
        </asp:RequiredFieldValidator>
      </td>
    </tr>
    <tr>
      <td align="center" colspan="2" style="color:Red;">
        <asp:Literal ID="CustomErrorMessage" runat="server" Visible="False" EnableViewState="False"></asp:Literal>
      </td>
    </tr>
  .
  .
  </asp:CreateUserWizard>

后面的代码:

protected void Page_Load(object sender, EventArgs e)
{

    if (!IsPostBack) {
        //Set up the Anti-Robot Question and Answer
        Label robotQuestion = (Label)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("AntiRobotQuestion");
        //Simulate randomly selecting a question and answer from a database table...
        robotQuestion.Text = "What is the capital of France";
        Session["AntiRobotAnswer"] = "Paris";
    }

}

protected void CreateUserWizard1_CreatingUser(object sender, LoginCancelEventArgs e)
{
    //Check the anti-robot Q & A
    TextBox robotAnswer = (TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("AntiRobotAnswer");
    if (robotAnswer.Text != (string)Session["AntiRobotAnswer"])
    {
        Literal errorMessage = (Literal)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("CustomErrorMessage");
        errorMessage.Text = "Wrong answer! Are you a robot?";
        errorMessage.Visible = true;
        e.Cancel = true;
    }

}

这是一种可接受的编码方式吗?在我看来,有两件事特别“不整洁”:

  1. 使用 FindControl 拉出对标记中控件的引用。
  2. 将预期答案存储在会话变量中。(它有多安全?)

编辑(2012-01-23)已经给出了一些有效的设计替代方案。但是,我有充分的理由使用这种问答技术(可能除了蜜罐的想法之外)。例如,与论坛主题相关的问题可以帮助防止垃圾邮件发送者和机器人。问题是:上面列出的代码是一种可接受的方式吗?来自 WinForms 背景,对我来说它看起来有点笨拙 - 但也许这就是 asp 应该看起来的样子。

4

1 回答 1

2

正如我所说,我不喜欢你要求去巴黎的想法。

  1. 最简单的方法是使用一个不可见的字段,看看机器人是否用数据填充它,蜜罐的想法http://haacked.com/archive/2007/09/11/honeypot-captcha.aspx

  2. 你也可以使用来自 asp.net 工具包的 NoBot http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/NoBot/NoBot.aspx

  3. 这篇 SO 文章还有许多其他想法实用的非基于图像的验证码方法?

于 2012-01-20T00:25:29.437 回答