1
0

E-Mail Validierung hinzugefügt

This commit is contained in:
2021-11-22 22:13:29 +01:00
parent 0736d54c87
commit e31aa18546
4 changed files with 106 additions and 1 deletions

View File

@@ -1,5 +1,6 @@
using System;
using System.Linq;
using System.Net;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
@@ -245,5 +246,60 @@ namespace AMWD.Common.Tests.Extensions
Assert.AreEqual(number, numberEn);
Assert.AreEqual(number, numberDe);
}
[TestMethod]
public void ShouldValidateEmailAddressWithoutRecordCheck()
{
// arrange
string validEmailWithoutTag = "test@gmail.com";
string validEmailWithTag = "test+tag@not.exists";
string invalidEmailWithoutTag = "<Test Account> test@gmail.com";
string invalidEmailWithTag = "<Test Account> test+tag@not.exists";
// act
bool validWithoutTag = validEmailWithoutTag.IsValidEmailAddress(checkRecordExists: false);
bool validWithTag = validEmailWithTag.IsValidEmailAddress(checkRecordExists: false);
bool invalidWithoutTag = !invalidEmailWithoutTag.IsValidEmailAddress(checkRecordExists: false);
bool invalidWithTag = !invalidEmailWithTag.IsValidEmailAddress(checkRecordExists: false);
// assert
Assert.IsTrue(validWithoutTag);
Assert.IsTrue(validWithTag);
Assert.IsTrue(invalidWithoutTag);
Assert.IsTrue(invalidWithTag);
}
[TestMethod]
public void ShouldValidateEmailAddressWithRecordCheck()
{
// arrange
string validEmail = "test@gmail.com";
string invalidEmail = "test@not.exists";
// act
bool valid = validEmail.IsValidEmailAddress(checkRecordExists: true);
bool invalid = !invalidEmail.IsValidEmailAddress(checkRecordExists: true);
// assert
Assert.IsTrue(valid);
Assert.IsTrue(invalid);
}
[TestMethod]
public void ShouldValidateEmailAddressWithRecordCheckDefinedNameservers()
{
// arrange
string validEmail = "test@gmail.com";
string invalidEmail = "test@not.exists";
var nameserver = new IPEndPoint(IPAddress.Parse("1.1.1.1"), 53);
// act
bool valid = validEmail.IsValidEmailAddress(new[] { nameserver });
bool invalid = !invalidEmail.IsValidEmailAddress(new[] { nameserver });
// assert
Assert.IsTrue(valid);
Assert.IsTrue(invalid);
}
}
}

View File

@@ -36,6 +36,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="DnsClient" Version="1.5.0" />
<PackageReference Include="Newtonsoft.Json" Version="10.0.1" />
<PackageReference Include="Unclassified.DeepConvert" Version="1.3.0" />
<PackageReference Include="Unclassified.NetRevisionTask" Version="0.4.1">

View File

@@ -1,8 +1,11 @@
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Text.RegularExpressions;
using DnsClient;
namespace System
{
@@ -137,5 +140,50 @@ namespace System
return decimal.Parse(decString, culture);
}
/// <summary>
/// Checks whether the given <see cref="string"/> is a valid <see cref="MailAddress"/>.
/// </summary>
/// <remarks>
/// You can enhance the check by requesting the MX record of the domain.
/// </remarks>
/// <param name="email">The email address as string to validate.</param>
/// <param name="checkRecordExists">A value indicating whether to resolve a MX record.</param>
/// <returns><c>true</c> when the email address is valid, other wise <c>false</c>.</returns>
public static bool IsValidEmailAddress(this string email, bool checkRecordExists = false)
=> email.IsValidEmailAddress(checkRecordExists ? new LookupClient() : null);
/// <summary>
/// Checks whether the given <see cref="string"/> is a valid <see cref="MailAddress"/>.
/// </summary>
/// <remarks>
/// The check is enhanced by a request for MX records on the defined <paramref name="nameservers"/>.
/// </remarks>
/// <param name="email">The email address as string to validate.</param>
/// <param name="nameservers">A list of <see cref="IPEndPoint"/>s of nameservers.</param>
/// <returns><c>true</c> when the email address is valid, other wise <c>false</c>.</returns>
public static bool IsValidEmailAddress(this string email, IEnumerable<IPEndPoint> nameservers)
=> email.IsValidEmailAddress(new LookupClient(nameservers.ToArray()));
private static bool IsValidEmailAddress(this string email, LookupClient lookupClient)
{
try
{
var mailAddress = new MailAddress(email);
bool isValid = mailAddress.Address == email;
if (isValid && lookupClient != null)
{
var result = lookupClient.Query(mailAddress.Host, QueryType.MX);
isValid &= result.Answers.MxRecords().Any();
}
return isValid;
}
catch
{
return false;
}
}
}
}

View File

@@ -8,7 +8,7 @@ To save time, they are all packed to NuGet packages.
| AMWD.Common | ![https://nuget.am-wd.de/packages/amwd.common/](https://img.shields.io/badge/dynamic/json?color=blue&label=AMWD.Common&query=%24.versions[-1%3A]&url=https%3A%2F%2Fnuget.am-wd.de%2Fv3%2Fpackage%2Famwd.common%2Findex.json&style=flat-square) |
| AMWD.Common.AspNetCore | ![https://nuget.am-wd.de/packages/amwd.common.aspnetcore/](https://img.shields.io/badge/dynamic/json?color=blue&label=AMWD.Common.AspNetCore&query=%24.versions[-1%3A]&url=https%3A%2F%2Fnuget.am-wd.de%2Fv3%2Fpackage%2Famwd.common.aspnetcore%2Findex.json&style=flat-square) |
| AMWD.Common.EntityFrameworkCore | ![https://nuget.am-wd.de/packages/amwd.common.entityframeworkcore/](https://img.shields.io/badge/dynamic/json?color=blue&label=AMWD.Common.EntityFrameworkCore&query=%24.versions[-1%3A]&url=https%3A%2F%2Fnuget.am-wd.de%2Fv3%2Fpackage%2Famwd.common.entityframeworkcore%2Findex.json&style=flat-square) |
| Build Pipeline | ![https://git.am-wd.de/AM.WD/common/-/commits/master](https://git.am-wd.de/AM.WD/common/badges/master/pipeline.svg?style=flat-square) |
| CI / CD | ![https://git.am-wd.de/AM.WD/common/-/pipelines](https://git.am-wd.de/AM.WD/common/badges/master/pipeline.svg?style=flat-square) ![https://git.am-wd.de/AM.WD/common/-/tree/master](https://git.am-wd.de/AM.WD/common/badges/master/coverage.svg?style=flat-square) |
## Documentation