垃圾邮件过滤:我从哪里开始

发布于 2025-01-03 02:58:37 字数 96 浏览 1 评论 0原文

我创建了一个新的 asp.net 站点(Web 表单,C#),并希望使其能够防止垃圾邮件通过文本框传入并添加到数据库中。有谁有关于如何实现这一点的良好链接?

谢谢

I have created a new asp.net site (web forms, c#) and looking to make it secure against spam coming through textboxes and being added to the database. Does anyone have any good links on how to implement this?

Thanks

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

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

发布评论

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

评论(3

来日方长 2025-01-10 02:58:37

我为此使用 ReCaptcha。
使用命令安装

Install-Package recaptcha  

您可以从 nuget 下载它或通过包管理控制台

 public class NoCache : ActionFilterAttribute
{

public class CaptchaValidatorAttribute : ActionFilterAttribute
{
    private const string CHALLENGE_FIELD_KEY = "recaptcha_challenge_field";
    private const string RESPONSE_FIELD_KEY = "recaptcha_response_field";
    private const string CAPTCHA_MODEL_KEY = "Captcha";

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        var captchaChallengeValue = filterContext.HttpContext.Request.Form[CHALLENGE_FIELD_KEY];
        var captchaResponseValue = filterContext.HttpContext.Request.Form[RESPONSE_FIELD_KEY];
        var captchaValidtor = new Recaptcha.RecaptchaValidator
        {
            PrivateKey = "key",
            RemoteIP = filterContext.HttpContext.Request.UserHostAddress,
            Challenge = captchaChallengeValue,
            Response = captchaResponseValue
        };

        var recaptchaResponse = captchaValidtor.Validate();

        if (!recaptchaResponse.IsValid)
        {
            filterContext.Controller
                .ViewData.ModelState
                .AddModelError(
                    CAPTCHA_MODEL_KEY,
                    "Entered text is invalid");
        }

        base.OnActionExecuting(filterContext);
    }
}

public static class CaptchaExtensions
{
    public static string GenerateCaptcha(this HtmlHelper helper)
    {
        var captchaControl = new Recaptcha.RecaptchaControl
        {
            ID = "recaptcha",
            Theme = "white",
            PublicKey = "key",
            PrivateKey = "key"
        };
        var htmlWriter = new HtmlTextWriter(new StringWriter());
        captchaControl.RenderControl(htmlWriter);
        return htmlWriter.InnerWriter.ToString();
    }
}

您可以

 @using (Html.BeginForm("activate_user", , FormMethod.Post))
{
@Html.HiddenFor(x => x.Email)
 <div class="captcha">
    @Html.Raw(@Html.GenerateCaptcha())
    <div style="text-align:center; margin-left:-25px;">
    @Html.ValidationMessage("Captcha")       
    </div>
 </div>       
 <input type="submit" class="signUpButton active activation" value="Activate" />
}

在控制器中使用 And :

    [ActionName("activate_user")]
    [CaptchaValidator]
    [HttpPost]
    public ActionResult ActivateUser(string email)
    {
        if (ModelState.IsValid && !string.IsNullOrEmpty(email))
        {
            FormsAuthentication.SetAuthCookie(email, false);
            Repository.ActivateUser(email);     
        }
        return View();
    }

I use ReCaptcha for this.
You can download it from nuget or install with

Install-Package recaptcha  

command through package management console

 public class NoCache : ActionFilterAttribute
{

public class CaptchaValidatorAttribute : ActionFilterAttribute
{
    private const string CHALLENGE_FIELD_KEY = "recaptcha_challenge_field";
    private const string RESPONSE_FIELD_KEY = "recaptcha_response_field";
    private const string CAPTCHA_MODEL_KEY = "Captcha";

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        var captchaChallengeValue = filterContext.HttpContext.Request.Form[CHALLENGE_FIELD_KEY];
        var captchaResponseValue = filterContext.HttpContext.Request.Form[RESPONSE_FIELD_KEY];
        var captchaValidtor = new Recaptcha.RecaptchaValidator
        {
            PrivateKey = "key",
            RemoteIP = filterContext.HttpContext.Request.UserHostAddress,
            Challenge = captchaChallengeValue,
            Response = captchaResponseValue
        };

        var recaptchaResponse = captchaValidtor.Validate();

        if (!recaptchaResponse.IsValid)
        {
            filterContext.Controller
                .ViewData.ModelState
                .AddModelError(
                    CAPTCHA_MODEL_KEY,
                    "Entered text is invalid");
        }

        base.OnActionExecuting(filterContext);
    }
}

public static class CaptchaExtensions
{
    public static string GenerateCaptcha(this HtmlHelper helper)
    {
        var captchaControl = new Recaptcha.RecaptchaControl
        {
            ID = "recaptcha",
            Theme = "white",
            PublicKey = "key",
            PrivateKey = "key"
        };
        var htmlWriter = new HtmlTextWriter(new StringWriter());
        captchaControl.RenderControl(htmlWriter);
        return htmlWriter.InnerWriter.ToString();
    }
}

Than you can use

 @using (Html.BeginForm("activate_user", , FormMethod.Post))
{
@Html.HiddenFor(x => x.Email)
 <div class="captcha">
    @Html.Raw(@Html.GenerateCaptcha())
    <div style="text-align:center; margin-left:-25px;">
    @Html.ValidationMessage("Captcha")       
    </div>
 </div>       
 <input type="submit" class="signUpButton active activation" value="Activate" />
}

And in controller:

    [ActionName("activate_user")]
    [CaptchaValidator]
    [HttpPost]
    public ActionResult ActivateUser(string email)
    {
        if (ModelState.IsValid && !string.IsNullOrEmpty(email))
        {
            FormsAuthentication.SetAuthCookie(email, false);
            Repository.ActivateUser(email);     
        }
        return View();
    }
满天都是小星星 2025-01-10 02:58:37

如果你没有垃圾邮件,我就不会担心。

话虽这么说,如果您确实收到了它,您想知道收到的是哪种垃圾邮件。假设您对有效输入有限制,根据模型验证输入应该可以防止大部分问题。如果任何事情都有效,或者由于某种原因你必须接受一切,你可以从蜜罐开始,这是一种简单、非侵入性的方法。

要实现蜜罐,您基本上需要添加一个字段,用 CSS 隐藏它,并在服务器端验证该字段是否为空。大多数垃圾邮件机器人都会填写所有字段,这将识别自动化程序何时提交了表单。

如果您发现这对于防止网站上的所有垃圾邮件无效,您需要查看正在通过的垃圾邮件类型并找到可以防止这种情况的方法。作为最后的手段,您可以采取侵入性操作,例如重新验证码。验证码的真正问题(如 Eric Lippert 简洁地指出) 是他们假设有罪,用户试图做坏事,这会对你的用户产生负面影响。

If you do not have spam, I wouldn't worry about it.

That being said, if you do have it, you want to know what kind of spam you are getting. Validating the input against the model should prevent most of it assuming you have restrictions on the valid input. If just about anything validates, or you have to accept everything for some reason, you can start with a honey pot, which is a simple, non intrusive method.

To implement a honey pot, you basically add a field, hide it with CSS and and validate that field is null on the server side. Most spam bots fill out all fields and this will identify when something automated has submitted the form.

If you find this ineffective in preventing all spam on your site, you need to see what kind of spam is getting through and find something that prevents that. As a last resort, you can move to intrusive actions such as recaptcha. The real issue with CAPTCHA's (as Eric Lippert's succinctly states it) is that they assume guilt, that the user is trying to do something bad, and that has a negative effect on your users.

怎会甘心 2025-01-10 02:58:37

我建议您使用适用于 WordPress 的 Growmap 反垃圾邮件插件 并将其转换为Javascript 和 PHP 实现可以在您的项目中使用。对于最终用户来说,它不像验证码那么烦人,而且它在阻止我的 WordPress 博客上的自动垃圾邮件方面非常有效。进行 ASP.NET/C# 转换应该不是什么大问题。我开始做一个,但我正在做的项目被取消了,所以我没有完成它。

当然,这对手动发送垃圾邮件没有帮助,因为有人花 5 美元让坐在第三世界网吧的人在数十个网站上输入废话。这也是许多其他反垃圾邮件系统(包括验证码)的问题。此活动主要是为了获取用于 SEO 目的的链接,因此筛选链接以进行审核或阻止其输入可以减少此活动。

I suggest that you take the Growmap Anti-Spam plugin for WordPress and convert its Javascript and PHP implementation to something you can use in your project. It's not as annoying as CAPTCHA to end users and it's been quite effective in stopping automated spam on my WordPress blogs. It shouldn't be a big deal to do the ASP.NET/C# conversion. I started on one but the project I was doing it for got canceled so I didn't complete it.

Of course, it won't help with manual spamming where somebody pays $5 for someone sitting in a third world Internet cafe to enter nonsense onto dozens of sites. This is also a problem for many other anti-spam systems, including CAPTCHA. This activity is primarily done to get links for SEO purposes so screening out links for moderation or preventing them from being entered can curtail this activity.

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