繁体   English   中英

如何限制用户一次又一次不通过会话从其IP地址在asp.net mvc中提交联系表单

[英]how to restrict user from submitting contact form again and again without session from its IP address in asp.net mvc

我想限制用户从其IP地址一次又一次提交联系表。 如何在asp.net MVC中做到这一点? 有谁能够帮助我? 我知道如何检索用户的IP,但是我不知道如何使用它来限制用户再次提交表单

我的控制器代码

string IP = String.Empty;

System.Web.HttpContext current = System.Web.HttpContext.Current;
string IPAddress = current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

if(!string.IsNullOrEmpty(IPAddress))
{
    string[] valAddress = IPAddress.Split('.');
    if(valAddress.Length != 0)
    {
        IP = valAddress[0];
    }
}

IP = current.Request.ServerVariables["REMOTE_ADDR"];

有几种方法可以做到这一点。 重要的问题是,您的工作规范是什么?

如果您开发公共网站,例如stackoverflow或facebook等,则应考虑在客户端和后端均实施工具验证。 如果您开发基于Intranet的Web应用程序,则客户端验证就足够了。

如果您的表单是简单的联系表单,则应使用Google ReCaptcha来防止自动代码,自动程序,以免炸毁数据库或电子邮件。

https://www.google.com/recaptcha/intro/v3beta.html

这是我为防止用户发送多个邮件而实现的方法。

[HttpPost]
        public ActionResult Index(YourViewModel model)
        {
            #region backend validation

            if (
                !GoogleReCAPTCHAHelper.Check(Request["g-recaptcha-response"],
                    ConfigurationManager.AppSettings["GoogleReCAPTCHASecret"]))
            {
                ModelState.AddModelError(string.Empty, "You have to confirm that you are not robot!");
                return View(model);
            }

            if ((from file in model.Files where file != null select file.FileName.Split('.')).Any(arr => arr[arr.Length - 1].ToLower() != "pdf"))
            {
                ModelState.AddModelError(string.Empty, "We only accept PDF files!");
                return View(model);
            }
            if (model.Files.Count() > 2)
            {
                ModelState.AddModelError(string.Empty,
                    "You have exceeded maximum file upload size. You can upload maximum 2 PDF file!");
                return View(model);
            }

            //// this stops the applicant sending the application multiple times within 5 minutes of a submition
            DateTime? lastSubmission = null;
            if (Request.Cookies["LastSubmission"] != null)
                lastSubmission = Convert.ToDateTime(Request.Cookies["LastSubmission"].Value);
            if (lastSubmission.HasValue && DateTime.Now < lastSubmission.Value.AddMinutes(5))
            {
                ModelState.AddModelError(string.Empty,
                    "ERROR: Your application has not been sent. This is due to receiving an application from you within the last five minutes. If you have made an error and wish to resend your application, please wait five minutes and then complete the application again.");
                return View(model);
            }

            if (!ModelState.IsValid)
            {
                ModelState.AddModelError(string.Empty, "Something went wrong!");
                return View(model);
            }

            #endregion
}

PS:model.Files是IEnumerable<HttpPostedFileBase>

最后但并非最不重要的一点是,当用户单击提交按钮时,您应该指示某种加载屏幕或禁用按钮以再次提交。 这样用户就可以了解网站上发生的事情...

这是验证的JavaScript示例。

function sendForm() {

    if (grecaptcha.getResponse() === "") {
        event.preventDefault();
        $("#robotErrorDiv").slideDown();
        return false;
    } else {
        $("#robotErrorDiv").slideUp();
    }

    $("#myForm").validate();
    if ($("#myForm").valid()) {
        $("#loaderGif").fadeIn();
        $("#submitButton").text("Sending...").attr("disabled", "disabled");
        $("#myForm").submit();
    }
    return true;
}

只需在HTML5本地存储中添加一个变量,并在用户每次单击按钮时对其进行验证。

即使关闭并重新打开浏览器,HTML 5 Local也可以无限期地存储数据。 但是,如果用户清除本地存储,则可以再次提交。 但是至少它将减少同一用户的点击数。

有关HTMl5本地存储的更多信息,请参阅https://www.w3schools.com/html/html5_webstorage.asp

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM