E-Mail Validierung hinzugefügt
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Net;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||||
|
|
||||||
@@ -245,5 +246,60 @@ namespace AMWD.Common.Tests.Extensions
|
|||||||
Assert.AreEqual(number, numberEn);
|
Assert.AreEqual(number, numberEn);
|
||||||
Assert.AreEqual(number, numberDe);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -36,6 +36,7 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<PackageReference Include="DnsClient" Version="1.5.0" />
|
||||||
<PackageReference Include="Newtonsoft.Json" Version="10.0.1" />
|
<PackageReference Include="Newtonsoft.Json" Version="10.0.1" />
|
||||||
<PackageReference Include="Unclassified.DeepConvert" Version="1.3.0" />
|
<PackageReference Include="Unclassified.DeepConvert" Version="1.3.0" />
|
||||||
<PackageReference Include="Unclassified.NetRevisionTask" Version="0.4.1">
|
<PackageReference Include="Unclassified.NetRevisionTask" Version="0.4.1">
|
||||||
|
|||||||
@@ -1,8 +1,11 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Net;
|
||||||
|
using System.Net.Mail;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
|
using DnsClient;
|
||||||
|
|
||||||
namespace System
|
namespace System
|
||||||
{
|
{
|
||||||
@@ -137,5 +140,50 @@ namespace System
|
|||||||
|
|
||||||
return decimal.Parse(decString, culture);
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ To save time, they are all packed to NuGet packages.
|
|||||||
| AMWD.Common |  |
|
| AMWD.Common |  |
|
||||||
| AMWD.Common.AspNetCore |  |
|
| AMWD.Common.AspNetCore |  |
|
||||||
| AMWD.Common.EntityFrameworkCore |  |
|
| AMWD.Common.EntityFrameworkCore |  |
|
||||||
| Build Pipeline |  |
|
| CI / CD |   |
|
||||||
|
|
||||||
## Documentation
|
## Documentation
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user