'How to safely build mailto link in C#?
How to create an Uri instance containing a mailto: link in ASP.NET 6/C# so it is immune to mailicious user input?
@{
var email = @"[email protected]"; // Unsafe value - supplied by a user
var linkTitle = email;
var linkDestination = new Uri("mailto:" + email); // Replace with a safer approach
}
<!-- Intended usage - should not need to care whether its an e-mail or web link -->
<a href="@linkDestination">@linkTitle</a>
Solution 1:[1]
You should validate your email variable, if this is a valid email.
One way to do this is to use regexp.
Another way is to use a System.ComponentModel.DataAnnotations.EmailAddressAttribute starting from .Net 4.5:
string email = @"[email protected]";
bool isValidEmail = email != null && new EmailAddressAttribute().IsValid(email);
Hope, this will help.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
