【问题标题】:Using reCaptcha with BlogEngine.net将 reCaptcha 与 BlogEngine.net 一起使用
【发布时间】:2009-08-11 13:53:39
【问题描述】:

我刚刚在我的网站上安装了 reCaptcha 并将控件放在我的评论帖子上,到目前为止一切顺利。

现在要验证 reCaptcha,它说只需执行 Page.IsValid。

但是 BlogEngine 使用 Ajax 和一些 JS 来发布它的 addComment 函数,如果我在那里测试我只是在状态栏中的页面上得到错误。

这里是 bloengine post 函数 -

/// <summary>
/// Processes a callback event that targets a control.
/// </summary>
/// <param name="eventArgument">A string that represents an event argument to pass to the event handler.</param>
public void RaiseCallbackEvent(string eventArgument)
{
    if (!BlogSettings.Instance.IsCommentsEnabled)
        return;

    string[] args = eventArgument.Split(new string[] { "-|-" }, StringSplitOptions.None);
    string author = args[0];
    string email = args[1];
    string website = args[2];
    string country = args[3];
    string content = args[4];
    bool notify = bool.Parse(args[5]);
    bool isPreview = bool.Parse(args[6]);
    string sentCaptcha = args[7];
    //If there is no "reply to" comment, args[8] is empty
    Guid replyToCommentID = String.IsNullOrEmpty(args[8]) ? Guid.Empty : new Guid(args[8]);

    string storedCaptcha = hfCaptcha.Value;

    Comment comment = new Comment();
    comment.Id = Guid.NewGuid();
    comment.ParentId = replyToCommentID;
    comment.Author = Server.HtmlEncode(author);
    comment.Email = email;
    comment.Content = Server.HtmlEncode(content);
    comment.IP = Request.UserHostAddress;
    comment.Country = country;
    comment.DateCreated = DateTime.Now;
    comment.Parent = Post;
    comment.IsApproved = !BlogSettings.Instance.EnableCommentsModeration;

    if (Page.User.Identity.IsAuthenticated)
        comment.IsApproved = true;

    if (website.Trim().Length > 0)
    {
        if (!website.ToLowerInvariant().Contains("://"))
            website = "http://" + website;

        Uri url;
        if (Uri.TryCreate(website, UriKind.Absolute, out url))
            comment.Website = url;
    }

    if (notify && !Post.NotificationEmails.Contains(email))
        Post.NotificationEmails.Add(email);
    else if (!notify && Post.NotificationEmails.Contains(email))
        Post.NotificationEmails.Remove(email);

    if (!isPreview)
    {
        Post.AddComment(comment);
        SetCookie(author, email, website, country);
    }

    string path = Utils.RelativeWebRoot + "themes/" + BlogSettings.Instance.Theme + "/CommentView.ascx";

    CommentViewBase control = (CommentViewBase)LoadControl(path);
    control.Comment = comment;
    control.Post = Post;

    using (StringWriter sw = new StringWriter())
    {
        control.RenderControl(new HtmlTextWriter(sw));
        _Callback = sw.ToString();
    }
}

我试着把 if(!Page.IsValid) return;但这从未奏效。

【问题讨论】:

  • 您能否发布 ASPX 和任何与验证码相关的 ASPX.CS 代码?我对 DNBE 相当熟悉,因为我有我的个人博客使用它,并为它编写了一些扩展。

标签: recaptcha


【解决方案1】:

reCaptcha 的默认 API 不适用于 AJAX 驱动的网页,尤其是当您替换 reCaptcha 所在的内容时。这里的问题是默认的 reCaptcha API。只需切换到 AJAX API,它也提供 here

【讨论】:

  • 如果我只使用 Ajax API,垃圾邮件发送者就不能禁用 JS 并绕过它吗?
  • 我想我应该检查一下 BlogEngine 代码,看看是否有正常的 cmets 发布方法并将默认 API 放在那里?
  • 使用 ajax 页面提交的页面将不起作用,如果禁用 JS 则不会被绕过。我会选择 ajax API(就像我对我的一个网站所做的那样)。
【解决方案2】:
var captchaChallengeValue = filterContext.HttpContext.Request.Form["recaptcha_challenge_field"];
var captchaResponseValue = filterContext.HttpContext.Request.Form["recaptcha_response_field"];
var captchaValidator = new Recaptcha.RecaptchaValidator
{
    PrivateKey = "private key here",
    RemoteIP = filterContext.HttpContext.Request.UserHostAddress,
    Challenge = captchaChallengeValue,
    Response = captchaResponseValue
};

var recaptchaResponse = captchaValidtor.Validate();

这就是我在另一个网站上的做法。我接受了输入的内容和响应,然后创建了一个新的 captchaValidator,它有一个方法可以检查响应是否有效。然后将其用作您的布尔值。

我正在使用 ASP.Net MVC。但是,我认为这个想法是相似的。

希望这会有所帮助。

【讨论】:

  • 我认为这是正确的,尽管我认为你的代码是正确的(我检查了调试器),如果我从我的评论方法返回 false,页面会由于 JS 错误而中断。不知道是不是return1.at上面帖子里说的有问题。
  • 我确定这就是问题所在。我只知道我尝试以与您相同的方式验证我的验证码,我必须这样做才能获得所需的布尔值。只是希望它能让你朝着正确的方向前进。
【解决方案3】:

现在 1.6.1 内置了对 reCaptcha 的支持 看 Enable ReCaptcha

【讨论】:

    猜你喜欢
    • 2010-11-15
    • 2019-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-25
    相关资源
    最近更新 更多