SqlMembershipProvider.GetUserNameByEmail(String) Metodo

Definizione

Ottiene il nome utente associato all'indirizzo di posta elettronica specificato.

public:
 override System::String ^ GetUserNameByEmail(System::String ^ email);
public override string GetUserNameByEmail (string email);
override this.GetUserNameByEmail : string -> string
Public Overrides Function GetUserNameByEmail (email As String) As String

Parametri

email
String

Indirizzo di posta elettronica da cercare.

Restituisce

Nome utente associato all'indirizzo di posta elettronica specificato. Se non viene individuato alcun nome utente, il metodo restituisce il valore null.

Eccezioni

email è di lunghezza superiore a 256 caratteri.

Nel database è presente più di un utente con lo stesso indirizzo di posta elettronica e RequiresUniqueEmail è true.

Esempio

Nell'esempio di codice seguente viene recuperato il nome utente per un indirizzo di posta elettronica specificato.

Nota

In questo esempio viene usata la Membership classe per chiamare l'oggetto SqlMembershipProvider specificato come defaultProvider nel file di Web.config. Se è necessario accedere al provider predefinito come tipo SqlMembershipProvider, è possibile eseguire il cast della Provider proprietà della Membership classe. Per accedere ad altri provider configurati come tipo di provider specifico, è possibile accedervi tramite il nome configurato con la proprietà della Membership classe e eseguirne il Providers cast come tipo di provider specifico.

<%@ 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>

Commenti

Questo metodo viene chiamato dalla Membership classe con un indirizzo di posta elettronica, che usa per recuperare un nome utente associato dal database SQL Server specificato nel file di configurazione dell'applicazione di ASP.NET (Web.config).

Se più utenti nel database di appartenenza hanno lo stesso indirizzo di posta elettronica, viene restituito il primo nome utente rilevato. È possibile impostare l'attributo su true nel file di configurazione dell'applicazione requiresUniqueEmail per assicurarsi che ogni indirizzo di posta elettronica nel database sia univoco.

Gli spazi iniziali e finali sono rimossi dal valore del parametro email.

Si applica a

Vedi anche