共用方式為


Membership.GetUserNameByEmail(String) 方法

定義

取得使用者名稱,其中使用者的電子郵件地址與指定的電子郵件地址相符。

public:
 static System::String ^ GetUserNameByEmail(System::String ^ emailToMatch);
public static string GetUserNameByEmail (string emailToMatch);
static member GetUserNameByEmail : string -> string
Public Shared Function GetUserNameByEmail (emailToMatch As String) As String

參數

emailToMatch
String

要搜尋的電子郵件地址。

傳回

使用者電子郵件地址與指定之電子郵件地址相符的使用者名稱。 如果找不到符合的項目,就會傳回 null

範例

下列程式代碼範例會擷取指定電子郵件地址的用戶名稱。

重要

此範例包含一個文本框,可接受用戶輸入,這是潛在的安全性威脅。 根據預設,ASP.NET Web 網頁會驗證使用者輸入未包含指令碼或 HTML 項目。 如需詳細資訊,請參閱 Script Exploits Overview (指令碼攻擊概觀)。

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Web.Security" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

public void Search_OnClick(object sender, EventArgs args)
{
  string username = Membership.GetUserNameByEmail(EmailTextBox.Text);

  if (username == null)
  {
    Msg.Text = "Email address " + Server.HtmlEncode(EmailTextBox.Text) + " is not found. Please reenter.";
  }
  else
  {
    Msg.Text = "The user name for " + Server.HtmlEncode(EmailTextBox.Text) + 
               " is " + Server.HtmlEncode(username) + ".";
  }

}

</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Sample: Retrieve Username By Email</title>
</head>
<body>

<form id="form1" runat="server">
  <h3>Forgot your Username? Search for it by email address.</h3>

  <asp:Label id="Msg" runat="server" ForeColor="maroon" /><br />

  Email address: <asp:Textbox id="EmailTextBox" Columns="30" runat="server" />
                  <asp:RequiredFieldValidator id="EmailRequiredValidator" runat="server"
                                        ControlToValidate="EmailTextBox" ForeColor="red"
                                        Display="Static" ErrorMessage="Required" /><br />

  <asp:Button id="SearchButton" Text="Search" 
              OnClick="Search_OnClick" runat="server" />

</form>

</body>
</html>
<%@ Page Language="VB" %>
<%@ Import Namespace="System.Web.Security" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

Public Sub Search_OnClick(sender As Object, args As EventArgs)

  Dim username As String = Membership.GetUserNameByEmail(EmailTextBox.Text)

  If username Is Nothing Then
    Msg.Text = "Email address " & Server.HtmlEncode(EmailTextBox.Text) & " is not found. Please reenter."
  Else
    Msg.Text = "The user name for " & Server.HtmlEncode(EmailTextBox.Text) & _
               " is " & Server.HtmlEncode(username) & "."
  End If

End Sub

</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Sample: Retrieve Username By Email</title>
</head>
<body>

<form id="form1" runat="server">
  <h3>Forgot your Username? Search for it by email address.</h3>

  <asp:Label id="Msg" runat="server" ForeColor="maroon" /><br />

  Email address: <asp:Textbox id="EmailTextBox" Columns="30" runat="server" />
                  <asp:RequiredFieldValidator id="EmailRequiredValidator" runat="server"
                                        ControlToValidate="EmailTextBox" ForeColor="red"
                                        Display="Static" ErrorMessage="Required" /><br />

  <asp:Button id="SearchButton" Text="Search" 
              OnClick="Search_OnClick" runat="server" />

</form>

</body>
</html>

備註

GetUserNameByEmail 在使用者不知道其用戶名稱,但知道其電子郵件地址的情況下,可用來擷取成員資格使用者的用戶名稱。 如果數據存放區中的多個使用者具有相同的電子郵件位址,則會傳回第一個用戶名稱。

前置和尾端空格會從所有參數值修剪。

適用於

另請參閱