将 Recaptcha 与 EPiServer XForms 结合使用

发布于 2024-10-08 18:16:05 字数 1246 浏览 5 评论 0原文

有人有在 EPiServer 中使用 Recaptcha 和 XForms 的经验吗?

我不知道将 Recaptcha 控件放在哪里以及如何使其工作。 ASP.NET 的示例代码如下。我应该把它放在哪里。我的猜测是在 FormControl_BeforeSubmitPostedData 中?

 <%@ Page Language="VB" %>
 <%@ Register TagPrefix="recaptcha" Namespace="Recaptcha" Assembly="Recaptcha" %>  
 <script runat=server%gt;       
 Sub btnSubmit_Click(ByVal sender As Object, ByVal e As EventArgs)           
    If Page.IsValid Then               
        lblResult.Text = "You Got It!"               
        lblResult.ForeColor = Drawing.Color.Green           
    Else               
        lblResult.Text = "Incorrect"               
        lblResult.ForeColor = Drawing.Color.Red           
    End If       
 End Sub   
 </script>   
 <html>   
 <body>       
 <form runat="server">           
     <asp:Label Visible=false ID="lblResult" runat="server" />
     <recaptcha:RecaptchaControl ID="recaptcha" runat="server" Theme="red"
                                 PublicKey="your_public_key" PrivateKey="your_private_key" />
     <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
 </form>   
 </body>   
 </html>

Does any one have experiense of using Recaptcha with XForms in EPiServer?

I don't know where to put the Recaptcha control and how to make it work. The sample code for ASP.NET is the code below. Where should i put it. My guess is in the FormControl_BeforeSubmitPostedData?

 <%@ Page Language="VB" %>
 <%@ Register TagPrefix="recaptcha" Namespace="Recaptcha" Assembly="Recaptcha" %>  
 <script runat=server%gt;       
 Sub btnSubmit_Click(ByVal sender As Object, ByVal e As EventArgs)           
    If Page.IsValid Then               
        lblResult.Text = "You Got It!"               
        lblResult.ForeColor = Drawing.Color.Green           
    Else               
        lblResult.Text = "Incorrect"               
        lblResult.ForeColor = Drawing.Color.Red           
    End If       
 End Sub   
 </script>   
 <html>   
 <body>       
 <form runat="server">           
     <asp:Label Visible=false ID="lblResult" runat="server" />
     <recaptcha:RecaptchaControl ID="recaptcha" runat="server" Theme="red"
                                 PublicKey="your_public_key" PrivateKey="your_private_key" />
     <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
 </form>   
 </body>   
 </html>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

望喜 2024-10-15 18:16:05

我有一些在 EPiServer 中修改 XForms 输出的经验,但之前我没有添加任何验证码控件。我希望我能帮助你到达那里!

我在 XFormControl.BeforeLoadingForm 事件上对 XForms 输出进​​行所有修改。您可以在 Global.asax.cs 中为此分配一个事件处理程序,或者创建一个静态初始化程序类,该类在有人第一次导航到表单页面时实例化(我的模板类继承自此初始化程序类。)我这样做只是因为我需要一个很好的可部署解决方案而不更改 Global.asax。无论如何,我离题了。

目前,我建议在 Global.asax.cs 中执行此操作,以便让您正常工作。 Global.asax.cs 中有示例代码,随 PublicTemplates 包一起安装。查找“Global XFrom Events”区域。

XForm 的“标记”通过 BeforeLoadingForm 事件参数公开。

e.FormDefinition

修改此字符串将更改表单的呈现输出,无论用户在 XForm 编辑器中创建什么。例如:

e.FormDefinition += "<asp:HyperLink runat=\"server\" Text=\"HyperLink\" />";

此示例显然将标记添加到当前存在的内容中,但如果您愿意,您可以完全转换原始标记。 (我使用正则表达式将表转换为 div/fieldset 标签)

我希望这可以帮助您找到解决方案。

Global.asax.cs 的示例代码

protected void Application_Start(Object sender, EventArgs e)
{
    XFormControl.ControlSetup += new EventHandler(XForm_ControlSetup);
}

public void XForm_ControlSetup(object sender, EventArgs e)
{
    XFormControl control = (XFormControl)sender;

    control.BeforeLoadingForm += new LoadFormEventHandler(XForm_BeforeLoadingForm);
}

public void XForm_BeforeLoadingForm(object sender, LoadFormEventArgs e)
{
    XFormControl formControl = (XFormControl)sender;

    //We set the validation group of the form to match our global validation group in the master page.
    formControl.ValidationGroup = "XForm";

    e.FormDefinition += "<asp:HyperLink runat=\"server\" NavigationUrl=\"#\" Text=\"HyperLink\" />";
}

编辑:

抱歉,上面的代码将帮助您将验证码控件集成到表单中,但完全错过了在提交表单之前实际检查验证码控件输入是否有效的部分!

我同意您对 XFormControl.BeforeSubmitPostedData 中的控件执行检查。然后,如果验证码无效,则输出错误消息并将 e.CancelSubmit 设置为 true。

对评论的回应:

我可能过于简化了事情,但这是我认为您需要的一个简单示例。在您的 XForm 用户控件代码中,您需要与此类似的内容:

protected override void OnInit(EventArgs e)
{
    base.OnInit(e);

    this.FormControl.BeforeLoadingForm += new LoadFormEventHandler(FormControl_BeforeLoadingForm);
    this.FormControl.BeforeSubmitPostedData += new SaveFormDataEventHandler(FormControl_BeforeSubmitPostedData);
}

void FormControl_BeforeLoadingForm(object sender, LoadFormEventArgs e)
{
    e.FormDefinition += "<recaptcha:RecaptchaControl runat=\"server\" id=\"CaptchaControl\" />";
}

void FormControl_BeforeSubmitPostedData(object sender, SaveFormDataEventArgs e)
{
    /* find the captcha control e.g. FormControl.FindControl("CaptchaControl") or otherwise
     * test if the control is valid
     * if not valid e.CancelSubmit = true (show error etc.)
     */
}

对于上面的示例,只要您在 web.config 中或模板/用户控件标记的顶部注册了 Recaptcha 控件,那么它就应该可以工作。您应该发现验证码控件只是添加到表单的末尾(可能不是您真正想要的位置,但是只要您能精确指出要将其插入到表单中的位置,就可以按照您自己的方式进行更改) e.FormDefinition 字符串。

其中“FormControl”是用户控件标记中的 XForm 控件

编辑 (23/12/2010):

验证码控件必须在 web.config 中注册,而不是在标记文件的顶部。

<add tagPrefix="recaptcha" namespace="Recaptcha" assembly="Recaptcha" />

I've had some experience with modifying the output of XForms within EPiServer, but I have not added any captcha controls before. I hope I can help you get there!

I do all modification of the XForms output on the XFormControl.BeforeLoadingForm event. You can assign an event handler to this either within the Global.asax.cs or create a static initializer class that instantiates the first time somebody navigates onto a form page (my template class inherits from this initialiser class.) I only did this because I needed a nice deployable solution without changing Global.asax. Anyway, I digress.

For now, I'd suggest doing it within the Global.asax.cs just to get you working. There is example code within the Global.asax.cs that is installed with the PublicTemplates pack. Look for the 'Global XFrom Events' region.

The 'markup' of the XForm is exposed though the BeforeLoadingForm event arguments.

e.FormDefinition

Modifying this string will change the rendered output of the form, regardless of what the user created within the XForm editor. For example:

e.FormDefinition += "<asp:HyperLink runat=\"server\" Text=\"HyperLink\" />";

This example obviously adds markup to what currently exists, but you can completely transform the original markup if you wish. (I use regex to transform the tables into div/fieldset tags)

I hope this helps you get to your solution.

Example code for Global.asax.cs

protected void Application_Start(Object sender, EventArgs e)
{
    XFormControl.ControlSetup += new EventHandler(XForm_ControlSetup);
}

public void XForm_ControlSetup(object sender, EventArgs e)
{
    XFormControl control = (XFormControl)sender;

    control.BeforeLoadingForm += new LoadFormEventHandler(XForm_BeforeLoadingForm);
}

public void XForm_BeforeLoadingForm(object sender, LoadFormEventArgs e)
{
    XFormControl formControl = (XFormControl)sender;

    //We set the validation group of the form to match our global validation group in the master page.
    formControl.ValidationGroup = "XForm";

    e.FormDefinition += "<asp:HyperLink runat=\"server\" NavigationUrl=\"#\" Text=\"HyperLink\" />";
}

EDIT:

Sorry, the above code will help you integrate the captcha control into your form but completely missed out the part of actually checking whether the captcha control input is valid before submitting the form!

I agree that you would perform a check on the control within XFormControl.BeforeSubmitPostedData. Then if the captcha is not valid output an error message and set e.CancelSubmit to true.

RESPONSE TO COMMENTS:

I may be simplifying things too much but this is a quick example of what I think you need. In your XForm user control code you need something similar to this:

protected override void OnInit(EventArgs e)
{
    base.OnInit(e);

    this.FormControl.BeforeLoadingForm += new LoadFormEventHandler(FormControl_BeforeLoadingForm);
    this.FormControl.BeforeSubmitPostedData += new SaveFormDataEventHandler(FormControl_BeforeSubmitPostedData);
}

void FormControl_BeforeLoadingForm(object sender, LoadFormEventArgs e)
{
    e.FormDefinition += "<recaptcha:RecaptchaControl runat=\"server\" id=\"CaptchaControl\" />";
}

void FormControl_BeforeSubmitPostedData(object sender, SaveFormDataEventArgs e)
{
    /* find the captcha control e.g. FormControl.FindControl("CaptchaControl") or otherwise
     * test if the control is valid
     * if not valid e.CancelSubmit = true (show error etc.)
     */
}

As for the above example, as long as you have the Recaptcha control registered in your web.config or at the top of your template/usercontrol markup then it should work. You should find that the captcha control is just added onto the end of the form (probably not where you really want it to be, but this can be changed in your own way as long as you can pinpoint where you want to insert it within the e.FormDefinition string.

Where "FormControl" is the XForm control within your user control markup.

EDIT (23/12/2010):

The captcha control must be registered within the web.config NOT at the top of the markup file.

<add tagPrefix="recaptcha" namespace="Recaptcha" assembly="Recaptcha" />
能怎样 2024-10-15 18:16:05

如果目标是防止垃圾邮件,更方便的方法是使用 Akismet 检查表单输入。我们已经为多个客户在 EPiServer 中的 XForms 中完成了此操作,效果非常好。

关于为什么验证码不适合可访问性的文章:
http://www.456bereastreet.com/archive/200709/provide_an_accessible_alternative_if_you_must_use_a_captcha/

If the goal is to prevent spam a more accessible way is to check the form input using Akismet. We have done this for XForms in EPiServer for multiple customers and it works great.

An article on why CAPTCHA isn't good for accessibility:
http://www.456bereastreet.com/archive/200709/provide_an_accessible_alternative_if_you_must_use_a_captcha/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文