c# - 阻止用户在表单中输入 URL

标签 c# asp.net-core asp.net-core-3.0

我有一个 ASP.Net Core 3.0 Web 应用程序,其中有一个带有多个输入字段的表单。输入字段绑定(bind)到模型,并且它们已经进行了一些验证。但是,在其中一个字段中,我想限制用户输入 URL 地址甚至电子邮件地址(但目前 URL 更重要)。

我的想法如下:在服务器端提交表单后,检查该字段中的文本,如果该文本包含某些 URL,则将其删除或使其无效(例如添加一些空格)。我的目标是,由于用户输入稍后将显示在网站中,因此限制任何 URL 处于事件状态或完全显示,因此如果其他用户正在检查该输入,则不会被欺骗点击某些恶意网站链接。

我的问题是:.Net Core 3(或以前的版本)上是否已经有一种机制可以自动检查用户输入中的 URL 并删除它们、使它们无效或给出验证错误?我本来打算自己编写整个逻辑,但如果这已经完成(在 .Net Core 或其他一些开源库中),那就更好了,并且可以节省我的一些精力。

我还想知道是否有一些自定义验证器甚至基本的 .Net 验证器正在执行此操作。我可以只在服务器端进行验证,但如果有机会我们对此进行客户端验证,那就更好了。

到目前为止,我没有任何具体的代码可以显示。我对一般情况感兴趣,所以如果它对你有帮助,你可以想象一个正常的 CRUD 形式(来自 VS 生成的形式)。

感谢任何帮助。

最诚挚的问候, 艾哈迈德

== 编辑 == 可能是我表达得不够清楚。我有兴趣查看用户输入的文本中是否包含一个或多个 URL。如果该文本中存在任何要删除它的 URL,则会以某种方式使其无效或给出验证错误。因此,如果用户输入以下文本:

"Here you can find some crazy deals - http://crazydeals.com/notsocrazydeals and you can buy some high quality toys"

要转向此:

"Here you can find some crazy deals - and you can buy some high quality toys"

或者这个

"Here you can find some crazy deals - h t t p : / / c r a z y d e a l s . c o m / n o t s o c r a z y d e a l s and you can buy some high quality toys"

最佳答案

正则表达式是解决这个问题的最佳方法,也许使用“https?:.*(?=\s)” 此代码将从字符串中删除所有 url:

Regex regx = new Regex("https?:.*(?=\s)", RegexOptions.IgnoreCase);

MatchCollection matches = regx.Matches(txt);

foreach (Match match in matches) {
    txt = txt.Replace(match.Value, "");

您还可以使用RegularExpressionAttribute使基于模式的模型输入无效。这样的属性在客户端和服务器端都会失效。

public class TestModel
{
    [RegularExpression(@"^((?!(https?:.*(?=\s))).)*$", ErrorMessage = "URL's are not allowed.")]
    public string Text { get; set; }
}

这是正则表达式属性的测试:

[TestMethod]
public void TestNotUrl()
{
    var modelFail = new TestModel { Text = "Here you can find some crazy deals - http://crazydeals.com/notsocrazydeals and you can buy some high quality toys" };
    var modelPass = new TestModel { Text = "Here you can find some crazy deals - crazydeals.com and you can buy some high quality toys" };

    var result = new List<ValidationResult>();
    var context = new ValidationContext(modelFail) { MemberName = "Text" };
    var expectNotValid = System.ComponentModel.DataAnnotations.Validator.TryValidateProperty(modelFail.Text, context, result);
    var expectValid = System.ComponentModel.DataAnnotations.Validator.TryValidateProperty(modelPass.Text, context, result);

    Assert.IsFalse(expectNotValid, "Expected modelFail.Text not to validate, as it contains a URL.");
    Assert.IsTrue(expectValid, "Expected modelPass.Text to validate, as it does not contain a URL.");
}

关于c# - 阻止用户在表单中输入 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58876564/

相关文章:

asp.net-core - 我只需要一份注册政策。任何人都知道 ASP.NET Core Azure AD B2C 配置的 PolicyId

c# - 在 Entity Framework 中订购包含的实体的最佳方式?

c# - 上传带有附加信息的文件(Angular 8 到 C# Core 3)

blazor - 如何禁用 Blazor 服务器端预渲染?

asp.net - 从 ASP.NET Core 2.0 迁移到 3.0

c# - 除非我切换选项卡,否则Toolstrip标签不会更新

c# - 如何在c#中序列化哈希表

c# - 为 .NET 库找到正确的组合根

c# - 异常 - 堆栈跟踪行号和消息不匹配

asp.net-core - 如何使用自定义服务在 ASP.NET Core 3.1 中注册身份验证提供程序?