Membership.GetUser Method

Definition

Gets the information for a membership user from the data source.

Overloads

GetUser(String, Boolean)

Gets the information from the data source for the specified membership user. Updates the last-activity date/time stamp for the user, if specified.

GetUser(Object, Boolean)

Gets the information from the data source for the membership user associated with the specified unique identifier. Updates the last-activity date/time stamp for the user, if specified.

GetUser(String)

Gets the information from the data source for the specified membership user.

GetUser(Boolean)

Gets the information from the data source for the current logged-on membership user. Updates the last-activity date/time stamp for the current logged-on membership user, if specified.

GetUser()

Gets the information from the data source and updates the last-activity date/time stamp for the current logged-on membership user.

GetUser(Object)

Gets the information from the data source for the membership user associated with the specified unique identifier.

GetUser(String, Boolean)

Gets the information from the data source for the specified membership user. Updates the last-activity date/time stamp for the user, if specified.

C#
public static System.Web.Security.MembershipUser GetUser(string username, bool userIsOnline);

Parameters

username
String

The name of the user to retrieve.

userIsOnline
Boolean

If true, updates the last-activity date/time stamp for the specified user.

Returns

A MembershipUser object representing the specified user. If the username parameter does not correspond to an existing user, this method returns null.

Exceptions

username contains a comma (,).

username is null.

Examples

The following code example retrieves the password for a specified user name and sends it to the email address for the specified user. The call to GetUser specifies that the last-activity date/time stamp for the user not be updated.

ASP.NET (C#)
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Web.Security" %>
<%@ Import Namespace="System.Net.Mail" %>
<!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 Page_Load(object sender, EventArgs args)
{
  if (!Membership.EnablePasswordRetrieval)
  {
    FormsAuthentication.RedirectToLoginPage();
  }

  Msg.Text = "";

  if (!IsPostBack)
  {
    Msg.Text = "Please enter a user name.";
  }
  else
  {
    VerifyUsername();
  }
}


public void VerifyUsername()
{
    MembershipUser user = Membership.GetUser(UsernameTextBox.Text, false);

    if (user == null)
    {
      Msg.Text = "The user name " + Server.HtmlEncode(UsernameTextBox.Text) + " was not found. Please check the value and re-enter.";

      QuestionLabel.Text = "";
      QuestionLabel.Enabled = false;
      AnswerTextBox.Enabled = false;
      EmailPasswordButton.Enabled = false;
    }
    else
    {
      QuestionLabel.Text = user.PasswordQuestion;
      QuestionLabel.Enabled = true;
      AnswerTextBox.Enabled = true;
      EmailPasswordButton.Enabled = true;
    }
}


public void EmailPassword_OnClick(object sender, EventArgs args)
{
  // Note: Returning a password in clear text using email is not recommended for
  // sites that require a high level of security.

  try
  {
    string password = Membership.Provider.GetPassword(UsernameTextBox.Text, AnswerTextBox.Text);
    MembershipUser u = Membership.GetUser(UsernameTextBox.Text);
    EmailPassword(u.Email, password);
    Msg.Text = "Your password was sent via email.";
  }
  catch (MembershipPasswordException e)
  {
    Msg.Text = "The password answer is incorrect. Please check the value and try again.";
  }
  catch (System.Configuration.Provider.ProviderException e)
  {
    Msg.Text = "An error occurred retrieving your password. Please check your values " +
               "and try again.";
  }
}


private void EmailPassword(string email, string password)
{
  try
  {
    MailMessage Message = new MailMessage("administrator", email);
    Message.Subject = "Your Password";
    Message.Body = "Your password is: " + Server.HtmlEncode(password);

    SmtpClient SmtpMail = new SmtpClient("SMTPSERVER");
    SmtpMail.Send(Message);
  }
  catch 
  {
    Msg.Text = "An exception occurred while sending your password. Please try again.";
  }
}

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

<form id="form1" runat="server">
  <h3>Retrieve Password</h3>

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

  Username: <asp:Textbox id="UsernameTextBox" Columns="30" runat="server" AutoPostBack="true" />
            <asp:RequiredFieldValidator id="UsernameRequiredValidator" runat="server"
                                        ControlToValidate="UsernameTextBox" ForeColor="red"
                                        Display="Static" ErrorMessage="Required" /><br />

  Password Question: <b><asp:Label id="QuestionLabel" runat="server" /></b><br />

  Answer: <asp:TextBox id="AnswerTextBox" Columns="60" runat="server" Enabled="false" />
          <asp:RequiredFieldValidator id="AnswerRequiredValidator" runat="server"
                                      ControlToValidate="AnswerTextBox" ForeColor="red"
                                      Display="Static" ErrorMessage="Required" Enabled="false" /><br />

  <asp:Button id="EmailPasswordButton" Text="Email My Password" 
              OnClick="EmailPassword_OnClick" runat="server" Enabled="false" />

</form>

</body>
</html>

Remarks

The GetUser method retrieves the user information from the data source and creates a MembershipUser object populated with the returned data.

If you use one of the GetUser overloads that does not take a username parameter, GetUser returns the information for the current logged-on membership user. The current logged-on membership user is identified by the Name of the user in the current HttpContext.

You can also specify whether you want GetUser to update the last-activity date/time stamp for the user being retrieved with the userIsOnline parameter. Of the GetUser overloads that do not take a userIsOnline parameter, Membership.GetUser implicitly updates the last-activity date/time stamp for the user. Membership.GetUser and GetUser do not.

See also

Applies to

GetUser(Object, Boolean)

Gets the information from the data source for the membership user associated with the specified unique identifier. Updates the last-activity date/time stamp for the user, if specified.

C#
public static System.Web.Security.MembershipUser GetUser(object providerUserKey, bool userIsOnline);

Parameters

providerUserKey
Object

The unique user identifier from the membership data source for the user.

userIsOnline
Boolean

If true, updates the last-activity date/time stamp for the specified user.

Returns

A MembershipUser object representing the user associated with the specified unique identifier.

Exceptions

providerUserKey is null.

Remarks

The GetUser method retrieves the user information from the data source and creates a MembershipUser object populated with the returned data. The user is identified with the unique identifier from the data source specified in the providerUserKey parameter.

See also

Applies to

GetUser(String)

Gets the information from the data source for the specified membership user.

C#
public static System.Web.Security.MembershipUser GetUser(string username);

Parameters

username
String

The name of the user to retrieve.

Returns

A MembershipUser object representing the specified user. If the username parameter does not correspond to an existing user, this method returns null.

Exceptions

username contains a comma (,).

username is null.

Examples

The following code example uses the GetUser method to return a MembershipUser object for the current logged-on user and updates the email address for the user.

ASP.NET (C#)
<%@ 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">

MembershipUser u;

public void Page_Load(object sender, EventArgs args)
{
  u = Membership.GetUser(User.Identity.Name);

  if (!IsPostBack)
  {
    EmailTextBox.Text = u.Email; 
  }
}

public void UpdateEmailButton_OnClick(object sender, EventArgs args)
{
  try
  {
    u.Email = EmailTextBox.Text;

    Membership.UpdateUser(u);
  
    Msg.Text = "User email updated.";
  }
  catch (System.Configuration.Provider.ProviderException e)
  {
    Msg.Text = e.Message;
  }
}

</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Sample: Update User E-Mail</title>
</head>
<body>

<form id="form1" runat="server">
  <h3>Update E-Mail Address for <%=User.Identity.Name%></h3>

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

  <table cellpadding="3" border="0">
    <tr>
      <td>Email Address:</td>
      <td><asp:TextBox id="EmailTextBox" MaxLength="128" Columns="30" runat="server" /></td>
      <td><asp:RequiredFieldValidator id="EmailRequiredValidator" runat="server"
                                    ControlToValidate="EmailTextBox" ForeColor="red"
                                    Display="Static" ErrorMessage="Required" /></td>
    </tr>
    <tr>
      <td></td>
      <td><asp:Button id="UpdateEmailButton" 
                      Text="Update Email" 
                      OnClick="UpdateEmailButton_OnClick" 
                      runat="server" /></td>
    </tr>
  </table>
</form>

</body>
</html>

Remarks

The GetUser method retrieves the user information from the data source and creates a MembershipUser object populated with the returned data. If you use one of the GetUser overloads that does not take a username parameter, GetUser returns the information for the current logged-on membership user. The current logged-on membership user is identified by the Name of the user in the current HttpContext.

You can also specify whether you want GetUser to update the last-activity date/time stamp for the user being retrieved with the userIsOnline parameter. Of the GetUser overloads that do not take a userIsOnline parameter, Membership.GetUser implicitly updates the last-activity date/time stamp for the user. Membership.GetUser and GetUser do not.

See also

Applies to

GetUser(Boolean)

Gets the information from the data source for the current logged-on membership user. Updates the last-activity date/time stamp for the current logged-on membership user, if specified.

C#
public static System.Web.Security.MembershipUser GetUser(bool userIsOnline);

Parameters

userIsOnline
Boolean

If true, updates the last-activity date/time stamp for the specified user.

Returns

A MembershipUser object representing the current logged-on user.

Exceptions

No membership user is currently logged in.

Examples

The following code example displays the user name for the current logged-on membership user in the ASP.NET page without updating the last-activity date/time stamp for the user.

ASP.NET (C#)
<%@ 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">

MembershipUser currentUser;

public void Page_Load()
{
  currentUser = Membership.GetUser(false);
}

</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Home Page</title>
</head>
<body>

<form id="form1" runat="server">
Welcome <b><%=currentUser.UserName%></b>. 
</form>

</body>
</html>

Remarks

The GetUser method retrieves the user information from the data source and creates a MembershipUser object populated with the returned data. If you use one of the GetUser overloads that does not take a username parameter, GetUser returns the information for the current logged-on membership user. The current logged-on membership user is identified by the Name of the user in the current HttpContext.

You can also specify whether you want GetUser to update the last-activity date/time stamp for the user being retrieved using the userIsOnline parameter. Of the GetUser overloads that do not take a userIsOnline parameter, GetUser() implicitly updates the last-activity date/time stamp for the user. GetUser(System.String) and GetUser(System.Object) do not.

See also

Applies to

GetUser()

Gets the information from the data source and updates the last-activity date/time stamp for the current logged-on membership user.

C#
public static System.Web.Security.MembershipUser GetUser();

Returns

A MembershipUser object representing the current logged-on user.

Exceptions

No membership user is currently logged in.

Examples

The following code example updates the last-activity date/time stamp for the current logged-on membership user and displays the user name in the ASP.NET page.

ASP.NET (C#)
<%@ 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">

MembershipUser currentUser;

public void Page_Load()
{
  currentUser = Membership.GetUser();
}

</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Home Page</title>
</head>
<body>

<form id="form1" runat="server">
Welcome <b><%=currentUser.UserName%></b>. 
</form>

</body>
</html>

Remarks

GetUser() retrieves the user information from the data source and creates a MembershipUser object populated with the returned data. If you use one of the GetUser overloads that does not take a username parameter, GetUser returns the information for the current logged-on membership user. The current logged-on membership user is identified by the Name of the user in the current HttpContext.

You can also specify whether you want GetUser to update the last-activity date/time stamp for the user being retrieved using the userIsOnline parameter. Of the GetUser overloads that do not take a userIsOnline parameter, GetUser() implicitly updates the last-activity date/time stamp for the user. GetUser(System.String) and GetUser(System.Object) do not.

See also

Applies to

GetUser(Object)

Gets the information from the data source for the membership user associated with the specified unique identifier.

C#
public static System.Web.Security.MembershipUser GetUser(object providerUserKey);

Parameters

providerUserKey
Object

The unique user identifier from the membership data source for the user.

Returns

A MembershipUser object representing the user associated with the specified unique identifier.

Exceptions

providerUserKey is null.

Remarks

The GetUser method retrieves the user information from the data source and creates a MembershipUser object populated with the returned data. The user is identified using the unique identifier from the data source specified using the providerUserKey parameter.

You can also specify whether you want GetUser to update the last-activity date/time stamp for the user being retrieved with the userIsOnline parameter. Of the GetUser overloads that do not take a userIsOnline parameter, GetUser() implicitly updates the last-activity date/time stamp for the user. GetUser(System.String) and GetUser(System.Object) do not

See also

Applies to