MembershipUser.UserName Propiedad

Definición

Obtiene el nombre de inicio de sesión del usuario de pertenencia.

public:
 virtual property System::String ^ UserName { System::String ^ get(); };
public virtual string UserName { get; }
member this.UserName : string
Public Overridable ReadOnly Property UserName As String

Valor de propiedad

Nombre de inicio de sesión del usuario de pertenencia.

Ejemplos

En el ejemplo de código siguiente se enlaza la UserName propiedad de todos los usuarios de pertenencia de una aplicación a un ListBox control y se usan etiquetas para mostrar información de pertenencia específica para un nombre de usuario seleccionado.

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

MembershipUserCollection users;

public void Page_Load()
{
  users = Membership.GetAllUsers();

  if (!IsPostBack)
  {
    // Bind users to ListBox.
    UsersListBox.DataSource = users;
    UsersListBox.DataBind();
  }


  // If a user is selected, show the properties for the selected user.

  if (UsersListBox.SelectedItem != null)
  {
    MembershipUser u = users[UsersListBox.SelectedItem.Value];

    EmailLabel.Text = u.Email;
    IsOnlineLabel.Text = u.IsOnline.ToString();
    LastLoginDateLabel.Text = u.LastLoginDate.ToString();
    CreationDateLabel.Text = u.CreationDate.ToString();
    LastActivityDateLabel.Text = u.LastActivityDate.ToString();
  }
}

</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Sample: View User Information</title>
</head>
<body>

<form runat="server" id="PageForm">

  <h3>View User Information</h3>

  <table border="0" cellspacing="4">
    <tr>
      <td valign="top">
        <asp:ListBox id="UsersListBox" DataTextField="Username" 
                     Rows="8" AutoPostBack="true" runat="server" />
      </td>
      <td valign="top">
        <table border="0" cellpadding="2" cellspacing="0">
          <tr>
           <td>Email:</td>
           <td><asp:Label runat="server" id="EmailLabel" /></td>
          </tr>
          <tr>
           <td>Is Online?:</td>
           <td><asp:Label runat="server" id="IsOnlineLabel" /></td>
          </tr>
          <tr>
           <td>LastLoginDate:</td>
           <td><asp:Label runat="server" id="LastLoginDateLabel" /></td>
          </tr>
          <tr>
           <td>CreationDate:</td>
           <td><asp:Label runat="server" id="CreationDateLabel" /></td>
          </tr>
          <tr>
           <td>LastActivityDate:</td>
           <td><asp:Label runat="server" id="LastActivityDateLabel" /></td>
          </tr>
        </table>
      </td>
    </tr>
  </table>

</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">

Dim users As MembershipUserCollection

Public Sub Page_Load()
  users = Membership.GetAllUsers()

  If Not IsPostBack Then
    ' Bind users to ListBox.
    UsersListBox.DataSource = users
    UsersListBox.DataBind()
  End If


  ' If a user is selected, show the properties for the selected user.

  If Not UsersListBox.SelectedItem Is Nothing Then
    Dim u As MembershipUser = users(UsersListBox.SelectedItem.Value)

    EmailLabel.Text = u.Email
    IsOnlineLabel.Text = u.IsOnline.ToString()
    LastLoginDateLabel.Text = u.LastLoginDate.ToString()
    CreationDateLabel.Text = u.CreationDate.ToString()
    LastActivityDateLabel.Text = u.LastActivityDate.ToString()
  End If
End Sub

</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Sample: View User Information</title>
</head>
<body>

<form runat="server" id="PageForm">

  <h3>View User Information</h3>

  <table border="0" cellspacing="4">
    <tr>
      <td valign="top">
        <asp:ListBox id="UsersListBox" DataTextField="Username" 
                     Rows="8" AutoPostBack="true" runat="server" />
      </td>
      <td valign="top">
        <table border="0" cellpadding="2" cellspacing="0">
          <tr>
           <td>Email:</td>
           <td><asp:Label runat="server" id="EmailLabel" /></td>
          </tr>
          <tr>
           <td>Is Online?:</td>
           <td><asp:Label runat="server" id="IsOnlineLabel" /></td>
          </tr>
          <tr>
           <td>LastLoginDate:</td>
           <td><asp:Label runat="server" id="LastLoginDateLabel" /></td>
          </tr>
          <tr>
           <td>CreationDate:</td>
           <td><asp:Label runat="server" id="CreationDateLabel" /></td>
          </tr>
          <tr>
           <td>LastActivityDate:</td>
           <td><asp:Label runat="server" id="LastActivityDateLabel" /></td>
          </tr>
        </table>
      </td>
    </tr>
  </table>

</form>

</body>
</html>

Comentarios

El usuario de UserName pertenencia se usa junto con la contraseña del usuario de pertenencia para autenticar al usuario de pertenencia.

de UserName un MembershipUser objeto identifica un usuario único para cada aplicación que usa Membership para la autenticación. La pertenencia ApplicationNameidentifica una aplicación. El mismo nombre de usuario se puede usar en varias aplicaciones para distintos usuarios. Como alternativa, se puede usar un único nombre de usuario para identificar un usuario para varias aplicaciones, siempre que las aplicaciones estén configuradas con el mismo ApplicationName.

Puede recuperar un MembershipUser objeto para un usuario de pertenencia pasando el nombre de usuario a una de las sobrecargas del GetUser método o como miembro de un MembershipUserCollection elemento devuelto desde el GetAllUsersmétodo , FindUsersByNameo FindUsersByEmail .

Puede recuperar el nombre de usuario de un usuario de pertenencia si se conoce la dirección de correo electrónico del usuario mediante el GetUserNameByEmail método .

Se aplica a

Consulte también