This documentation is archived and is not being maintained.

SqlMembershipProvider.GetUser Method (String, Boolean)

Returns information from the SQL Server membership database for a user and provides an option to update the last activity date/time stamp for the user.

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

'Declaration
Public Overrides Function GetUser ( _
	username As String, _
	userIsOnline As Boolean _
) As MembershipUser
'Usage
Dim instance As SqlMembershipProvider 
Dim username As String 
Dim userIsOnline As Boolean 
Dim returnValue As MembershipUser 

returnValue = instance.GetUser(username, _
	userIsOnline)

Parameters

username
Type: System.String

The name of the user to get information for.

userIsOnline
Type: System.Boolean

true to update the last activity date/time stamp for the user; false to return user information without updating the last activity date/time stamp for the user.

Return Value

Type: System.Web.Security.MembershipUser
A MembershipUser object representing the specified user. If no user is found in the database for the specified username value, Nothing is returned.

ExceptionCondition
System.ArgumentException

username exceeds 256 characters.

- or -

username contains a comma.

System.ArgumentNullException

username is Nothing.

This method is called by the Membership class to retrieve user information from the SQL Server database specified in the ASP.NET application's configuration file (Web.config).

If userIsOnline is true, the last activity date/time stamp for the user is updated to the current date and time. This is reflected in the LastActivityDate and IsOnline properties and in the value returned by GetNumberOfUsersOnline.

Leading and trailing spaces are trimmed from the username parameter value.

The following code example uses the GetUser method to determine whether a user exists before retrieving the password for the user.

NoteNote:

This example uses the Membership class to call the SqlMembershipProvider specified as the defaultProvider in the Web.config file. If you need to access the default provider as the type SqlMembershipProvider, you can cast the Provider property of the Membership class. To access other configured providers as a specific provider type, you can access them by their configured name with the Providers property of the Membership class and cast them as the specific provider type.

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

.NET Framework

Supported in: 3.5, 3.0, 2.0
Show: