This documentation is archived and is not being maintained.

MembershipUser.UserName Property

Gets the logon name of the membership user.

Namespace:  System.Web.Security
Assembly:  System.Web (in System.Web.dll)

'Declaration
Public Overridable ReadOnly Property UserName As String
'Usage
Dim instance As MembershipUser 
Dim value As String 

value = instance.UserName

Property Value

Type: System.String
The logon name of the membership user.

A membership user's UserName is used in conjunction with the membership user's password to authenticate the membership user.

The UserName of a MembershipUser identifies a unique user for each application that uses Membership for authentication. An application is identified by the membership ApplicationName. The same user name can be used in multiple applications for different users. Alternatively, a single user name can be used to identify a user for multiple applications, provided the applications are configured with the same ApplicationName.

You can retrieve a MembershipUser object for a membership user by passing the user name to one of the overloads of the GetUser method, or as a member of a MembershipUserCollection returned from the GetAllUsers, FindUsersByName, or FindUsersByEmail method.

You can retrieve the user name for a membership user if the e-mail address for the user is known, by using the GetUserNameByEmail method.

The following code example binds the UserName property of all the membership users in an application to a ListBox control and uses labels to show specific membership information for a selected user name.

<%@ 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>E-mail:</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>

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0
Show: