Membership.GetUser Method (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.
Assembly: System.Web (in System.Web.dll)
'Declaration Public Shared Function GetUser ( _ username As String, _ userIsOnline As Boolean _ ) As MembershipUser 'Usage Dim username As String Dim userIsOnline As Boolean Dim returnValue As MembershipUser returnValue = Membership.GetUser(username, _ userIsOnline)
Parameters
- username
- Type: System.String
The name of the user to retrieve.
- userIsOnline
- Type: System.Boolean
If true, updates the last-activity date/time stamp for the specified user.
Return Value
Type: System.Web.Security.MembershipUserA MembershipUser object representing the specified user.
| Exception | Condition |
|---|---|
| ArgumentException | username contains a comma (,). |
| ArgumentNullException | username is Nothing. |
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.
The following code example retrieves the password for a specified user name and sends it to the e-mail address for the specified user. The call to GetUser specifies that the last-activity date/time stamp for the user not be updated.
<%@ Page Language="VB" %> <%@ 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 Sub Page_Load(ByVal sender As Object, ByVal args As EventArgs) If Not Membership.EnablePasswordRetrieval Then FormsAuthentication.RedirectToLoginPage() End If Msg.Text = "" If Not IsPostBack Then Msg.Text = "Please enter a user name." Else VerifyUsername() End If End Sub Private Sub VerifyUsername() Dim user As MembershipUser = Membership.GetUser(UsernameTextBox.Text, False) If user Is Nothing Then 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 End If End Sub Public Sub EmailPassword_OnClick(ByVal sender As Object, ByVal args As EventArgs) ' Note: Returning a password in clear text using e-mail is not recommended for ' sites that require a high level of security. Try Dim password As String = Membership.Provider.GetPassword(UsernameTextBox.Text, AnswerTextBox.Text) Dim u As MembershipUser = Membership.GetUser(UsernameTextBox.Text) EmailPassword(u.Email, password) Msg.Text = "Your password was sent via e-mail." Catch e As MembershipPasswordException Msg.Text = "The password answer is incorrect. Please check the value and try again." Catch e As System.Configuration.Provider.ProviderException Msg.Text = "An error occurred retrieving your password. Please check your values " & _ "and try again." End Try End Sub Private Sub EmailPassword(ByVal email As String, ByVal password As String) Try Dim Message As MailMessage = New MailMessage("administrator", email) Message.Subject = "Your Password" Message.Body = "Your password is: " & Server.HtmlEncode(password) Dim SmtpMail As SmtpClient = New SmtpClient("SMTPSERVER") SmtpMail.Send(Message) Catch Msg.Text = "An exception occurred while sending your password. Please try again." End Try End Sub </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>
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.