MembershipUser Class (System.Web.Security)

Switch View :
ScriptFree
.NET Framework Class Library
MembershipUser Class

Exposes and updates membership user information in the membership data store.

Inheritance Hierarchy

System.Object
  System.Web.Security.MembershipUser
    System.Web.Security.ActiveDirectoryMembershipUser

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

Visual Basic
<SerializableAttribute> _
Public Class MembershipUser
C#
[SerializableAttribute]
public class MembershipUser
Visual C++
[SerializableAttribute]
public ref class MembershipUser
F#
[<SerializableAttribute>]
type MembershipUser =  class end

The MembershipUser type exposes the following members.

Constructors

  Name Description
Protected method MembershipUser() Creates a new instance of a MembershipUser object for a class that inherits the MembershipUser class.
Public method MembershipUser(String, String, Object, String, String, String, Boolean, Boolean, DateTime, DateTime, DateTime, DateTime, DateTime) Creates a new membership user object with the specified property values.
Top
Properties

  Name Description
Public property Comment Gets or sets application-specific information for the membership user.
Public property CreationDate Gets the date and time when the user was added to the membership data store.
Public property Email Gets or sets the e-mail address for the membership user.
Public property IsApproved Gets or sets whether the membership user can be authenticated.
Public property IsLockedOut Gets a value indicating whether the membership user is locked out and unable to be validated.
Public property IsOnline Gets whether the user is currently online.
Public property LastActivityDate Gets or sets the date and time when the membership user was last authenticated or accessed the application.
Public property LastLockoutDate Gets the most recent date and time that the membership user was locked out.
Public property LastLoginDate Gets or sets the date and time when the user was last authenticated.
Public property LastPasswordChangedDate Gets the date and time when the membership user's password was last updated.
Public property PasswordQuestion Gets the password question for the membership user.
Public property ProviderName Gets the name of the membership provider that stores and retrieves user information for the membership user.
Public property ProviderUserKey Gets the user identifier from the membership data source for the user.
Public property UserName Gets the logon name of the membership user.
Top
Methods

  Name Description
Public method ChangePassword Updates the password for the membership user in the membership data store.
Public method ChangePasswordQuestionAndAnswer Updates the password question and answer for the membership user in the membership data store.
Public method Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected method Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public method GetPassword() Gets the password for the membership user from the membership data store.
Public method GetPassword(String) Gets the password for the membership user from the membership data store.
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method ResetPassword() Resets a user's password to a new, automatically generated password.
Public method ResetPassword(String) Resets a user's password to a new, automatically generated password.
Public method ToString Returns the user name for the membership user. (Overrides Object.ToString().)
Public method UnlockUser Clears the locked-out state of the user so that the membership user can be validated.
Top
Remarks

The MembershipUser object is used to represent a single membership user in the membership data store. It exposes information about the membership user such as the e-mail address, and provides functionality for the membership user such as the ability to change or reset his or her password.

Note Note

If you are not familiar with the membership features of ASP.NET, see Introduction to Membership before continuing. For a list of other topics related to membership, see Managing Users by Using Membership.

A MembershipUser object is returned by the GetUser and CreateUser methods or as part of a MembershipUserCollection returned by the GetAllUsers, FindUsersByName, and FindUsersByEmail methods.

A MembershipUser object is required by the UpdateUser method when you want to update the information for an existing membership user.

Examples

The following code example updates the e-mail address for a user.

Security note Security Note

This example contains a text box that accepts user input, which is a potential security threat. By default, ASP.NET Web pages validate that user input does not include script or HTML elements. For more information, see Script Exploits Overview.

Visual Basic

<%@ 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 u As MembershipUser

Public Sub Page_Load(sender As Object, args As EventArgs)

  u = Membership.GetUser(User.Identity.Name)

  If Not IsPostBack Then EmailTextBox.Text = u.Email

End Sub

Public Sub UpdateEmailButton_OnClick(sender As Object, args As EventArgs)

  Try
    u.Email = EmailTextBox.Text

    Membership.UpdateUser(u)

    Msg.Text = "User e-mail updated."
  Catch e As System.Configuration.Provider.ProviderException
    Msg.Text = e.Message
  End Try

End Sub

</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>E-mail 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 E-mail" 
                      OnClick="UpdateEmailButton_OnClick" 
                      runat="server" /></td>
    </tr>
  </table>
</form>

</body>
</html>


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 e-mail 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>E-mail 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 E-mail" 
                      OnClick="UpdateEmailButton_OnClick" 
                      runat="server" /></td>
    </tr>
  </table>
</form>

</body>
</html>


Version Information

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4
Platforms

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
See Also

Reference

Other Resources

Community Content

MeKaris
Membership classes moved to System.Web.ApplicationServices Assembly
Please note all Membership related classes were moved to System.Web.ApplicationServices Assembly. The following are the classes in the Assembly: System.Web.Configuration.MembershipPasswordCompatibilityMode (Enum), System.Web.Security.MembershipCreateStatus, System.Web. Security.MembershipCreateUserException, System.Web. Security.MembershipPasswordException, System.Web.Security.MembershipPasswordFormat (Enum), System.Web.Security.MembershipProvider, System.Web.Security.MembershipProviderCollection, System.Web.Security.MembershipUser, System.Web.Security.MembershipUserCollection, System.Web.Security.RoleProvider, System.Web.Security.ValidatePasswordEventArgs.