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 网页验证用户输入是否不包含脚本或 HTML 元素。 有关详细信息,请参阅脚本侵入概述

<%@ 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 在用户不知道其用户名但知道其电子邮件地址的情况下,可用于检索成员身份用户的用户名。 如果数据存储中的多个用户具有相同的电子邮件地址,则返回遇到的第一个用户名。

从所有参数值中剪裁前导空格和尾随空格。

适用于

另请参阅