asp.net - 向 CreateUserWizard 添加 "anti-robot"增强功能

标签 asp.net asp.net-membership createuserwizard

我想向 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. 将预期答案存储在 session 变量中。 (安全性如何?)

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

最佳答案

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

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

  2. 您还可以使用 asp.net 工具包中的 NoBot http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/NoBot/NoBot.aspx

  3. 这篇文章还有很多其他想法 Practical non-image based CAPTCHA approaches?

关于asp.net - 向 CreateUserWizard 添加 "anti-robot"增强功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8919231/

相关文章:

c# - ASP.Net WEB API 无法从移动设备发布大型 JSON 数据

asp.net - IsAuthenticated 是假的!奇怪的行为+评论问题

使用浏览器后退按钮进行 ASP.NET 身份验证登录和注销

asp.net - 弄清楚为什么asp.net身份验证票证即将到期

asp.net - 是否可以使用 ASP.NET 全局阻止有人访问我的网站时丢弃的所有 cookie(包括第 3 方 cookie)?

c# - .NET MVC 的 jQuery getJSON 不工作

asp.net - 帐户创建后显示消息

c# - 创建用户向导 - 用户代码未处理空引用异常

c# - Findcontrol 属性在 createUserWizard 中不起作用

JavaScript,添加而不是替换