[研究]單一或多個 Email 格式驗證 (使用ASP.NET和 C#)
2022-06-10
續這篇,前端的檢查,對於駭客是可以用Burp Suite或其他工具或手法繞過,所以不管前端是否檢查,若要檢查,伺服器端的檢查是必須的。
[研究]單一或多個 Email 格式驗證 (使用 HTML5)
https://shaurong.blogspot.com/2022/06/email-html5.html
環境:Visual Studio 2022 + ASP.NET + WebForm + Web Application + C#
********************************************************************************
前端驗證 (瀏覽器端驗證)
參考這篇,可用 RegularExpressionValidator 於前端驗證
https://docs.microsoft.com/zh-tw/aspnet/web-forms/overview/older-versions-security/roles/role-based-authorization-cs
Default.aspx 中部份內容
<asp:TextBox runat="server" ID="Email" Text='<%# Bind("Email") %>'></asp:TextBox> <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="Email" Display="Dynamic" ErrorMessage="You must provide an email address." SetFocusOnError="True">*</asp:RequiredFieldValidator> <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="Email" Display="Dynamic" ForeColor="Red" ErrorMessage="Email Address 格式錯誤,請修正。" SetFocusOnError="True" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">* </asp:RegularExpressionValidator> |
********************************************************************************
伺服器端驗證方法 (後端驗證)
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication3.Default" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> </head> <body> <form id="form1" runat="server"> Single Email:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br /> <asp:Button ID="Button1" runat="server" Text="Send" OnClick="Button1_Click" /><br /> <asp:Label ID="Label1" runat="server"></asp:Label><br /> <br /> Multiple Email:<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br /> <asp:Button ID="Button2" runat="server" Text="Send" OnClick="Button2_Click" /><br /> <asp:Label ID="Label2" runat="server"></asp:Label><br /> </form> </body> </html> |
Default.aspx.cs
using System; namespace WebApplication3 { public partial class Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { bool isValidEmail = Common.IsValidEmail(TextBox1.Text); if (isValidEmail == true) Label1.Text = "Email 格式驗證通過。"; else Label1.Text = "Email 格式驗證失敗。"; } protected void Button2_Click(object sender, EventArgs e) { bool isValidEmailList = Common.IsValidEmailList(TextBox2.Text); if (isValidEmailList == true) Label2.Text = "Email List 格式驗證通過。"; else Label2.Text = "Email List 格式驗證失敗。"; } } } |
Common.cs
using System; using System.Globalization; using System.Text.RegularExpressions; namespace WebApplication3 { public class Common { // 如何:確認字串是否為有效的電子郵件格式 (C#) // https://msdn.microsoft.com/zh-tw/library/01escwtf(v=vs.110).aspx // https://docs.microsoft.com/zh-tw/dotnet/standard/base-types/how-to-verify-that-strings-are-in-valid-email-format // How to: Verify That Strings Are in Valid E-Mail Format | Microsoft Docs (VB) // https://docs.microsoft.com/en-us/previous-versions/dotnet/netframework-4.0/01escwtf(v=vs.100) // How to verify that strings are in valid email format | Microsoft Docs (C#) |
實際測試
(完)
沒有留言:
張貼留言