Captcha コントロールのよりアクセスしやすい代替手段として、「反ロボット」の質問を 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;
}
}
これは、これをコーディングする受け入れ可能な方法ですか? 特に 2 つの点は、私には少し「乱雑」に見えます。
- マークアップ内のコントロールへの参照を引き出すための FindControl の使用。
- 予想される回答をセッション変数に格納します。(どのくらい安全ですか?)
編集 (2012-01-23) いくつかの有効な設計の代替案が示されています。ただし、この質問と回答の手法を使用する正当な理由があります (おそらくハニーポットのアイデアに加えて)。たとえば、フォーラムの主題に関連する質問は、人間のスパマーやボットを防ぐのに役立ちます. 問題は、上記のコードはこれを行うための受け入れ可能な方法ですか? WinForms のバックグラウンドから来て、私には少しぎこちないように見えますが、それが asp の本来の姿なのかもしれません。