This documentation is archived and is not being maintained.

MembershipUser Constructor (String, String, Object, String, String, String, Boolean, Boolean, DateTime, DateTime, DateTime, DateTime, DateTime)

Creates a new membership user object with the specified property values.

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

public MembershipUser(
	string providerName,
	string name,
	Object providerUserKey,
	string email,
	string passwordQuestion,
	string comment,
	bool isApproved,
	bool isLockedOut,
	DateTime creationDate,
	DateTime lastLoginDate,
	DateTime lastActivityDate,
	DateTime lastPasswordChangedDate,
	DateTime lastLockoutDate
)

Parameters

providerName
Type: System.String
The ProviderName string for the membership user.
name
Type: System.String
The UserName string for the membership user.
providerUserKey
Type: System.Object
The ProviderUserKey identifier for the membership user.
email
Type: System.String
The Email string for the membership user.
passwordQuestion
Type: System.String
The PasswordQuestion string for the membership user.
comment
Type: System.String
The Comment string for the membership user.
isApproved
Type: System.Boolean
The IsApproved value for the membership user.
isLockedOut
Type: System.Boolean
true to lock out the membership user; otherwise, false.
creationDate
Type: System.DateTime
The CreationDate DateTime object for the membership user.
lastLoginDate
Type: System.DateTime
The LastLoginDate DateTime object for the membership user.
lastActivityDate
Type: System.DateTime
The LastActivityDate DateTime object for the membership user.
lastPasswordChangedDate
Type: System.DateTime
The LastPasswordChangedDate DateTime object for the membership user.
lastLockoutDate
Type: System.DateTime
The LastLockoutDate DateTime object for the membership user.

ExceptionCondition
ArgumentException

providerName is null.

-or-

providerName is not found in the Providers collection.

PlatformNotSupportedException

The constructor is not available. This can occur if the application targets the .NET Framework 4 Client Profile. To prevent this exception, derive your class from the type and then call the default protected constructor, or change the application to target the full version of the .NET Framework.

Creating a new MembershipUser object does not add a new membership user object to the membership data store. To add a new membership user to the membership data store, use the CreateUser method. Note that the CreateUser method returns a MembershipUser object for the membership user added to the data store.

MembershipUser objects can be constructed in application code for use with the UpdateUser method. Alternatively, you can also pass a MembershipUser object returned from the CreateUser, GetUser, GetAllUsers, FindUsersByName, or FindUsersByEmail method to the UpdateUser method as well.

MembershipUser objects are also commonly constructed by membership-provider implementations for the CreateUser, GetUser, GetAllUsers, FindUsersByName, and FindUsersByEmail methods.

The name, email, and passwordQuestion parameters are all trimmed before being used.

The following code example shows an implementation of the CreateUser method for a membership provider. The method constructs a MembershipUser object that is returned when the user is successfully added to the data store.


public override MembershipUser CreateUser(string username,
         string password,
         string email,
         string passwordQuestion,
         string passwordAnswer,
         bool isApproved,
         object providerUserKey,
         out MembershipCreateStatus status)
{
  ValidatePasswordEventArgs args =
    new ValidatePasswordEventArgs(username, password, true);

  OnValidatingPassword(args);

  if (args.Cancel)
  {
    status = MembershipCreateStatus.InvalidPassword;
    return null;
  }


  if (RequiresUniqueEmail && GetUserNameByEmail(email) != "")
  {
    status = MembershipCreateStatus.DuplicateEmail;
    return null;
  }

  MembershipUser u = GetUser(username, false);

  if (u == null)
  {
    DateTime createDate = DateTime.Now;

    if (providerUserKey == null)
    {
      providerUserKey = Guid.NewGuid();
    }
    else
    {
      if (!(providerUserKey is Guid))
      {
        status = MembershipCreateStatus.InvalidProviderUserKey;
        return null;
      }
    }

    OdbcConnection conn = new OdbcConnection(ConnectionString);
    OdbcCommand cmd = new OdbcCommand("INSERT INTO Users " +
          " (PKID, Username, Password, Email, PasswordQuestion, " +
          " PasswordAnswer, IsApproved," +
          " Comment, CreationDate, LastPasswordChangedDate, LastActivityDate," +
          " ApplicationName, IsLockedOut, LastLockedOutDate," +
          " FailedPasswordAttemptCount, FailedPasswordAttemptWindowStart, " +
          " FailedPasswordAnswerAttemptCount, FailedPasswordAnswerAttemptWindowStart)" +
          " Values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", conn);

    cmd.Parameters.Add("@PKID", OdbcType.UniqueIdentifier).Value = providerUserKey;
    cmd.Parameters.Add("@Username", OdbcType.VarChar, 255).Value = username;
    cmd.Parameters.Add("@Password", OdbcType.VarChar, 255).Value = EncodePassword(password);
    cmd.Parameters.Add("@Email", OdbcType.VarChar, 128).Value = email;
    cmd.Parameters.Add("@PasswordQuestion", OdbcType.VarChar, 255).Value = passwordQuestion;
    cmd.Parameters.Add("@PasswordAnswer", OdbcType.VarChar, 255).Value = EncodePassword(passwordAnswer);
    cmd.Parameters.Add("@IsApproved", OdbcType.Bit).Value = isApproved;
    cmd.Parameters.Add("@Comment", OdbcType.VarChar, 255).Value = "";
    cmd.Parameters.Add("@CreationDate", OdbcType.DateTime).Value = createDate;
    cmd.Parameters.Add("@LastPasswordChangedDate", OdbcType.DateTime).Value = createDate;
    cmd.Parameters.Add("@LastActivityDate", OdbcType.DateTime).Value = createDate;
    cmd.Parameters.Add("@ApplicationName", OdbcType.VarChar, 255).Value = pApplicationName;
    cmd.Parameters.Add("@IsLockedOut", OdbcType.Bit).Value = false;
    cmd.Parameters.Add("@LastLockedOutDate", OdbcType.DateTime).Value = createDate;
    cmd.Parameters.Add("@FailedPasswordAttemptCount", OdbcType.Int).Value = 0;
    cmd.Parameters.Add("@FailedPasswordAttemptWindowStart", OdbcType.DateTime).Value = createDate;
    cmd.Parameters.Add("@FailedPasswordAnswerAttemptCount", OdbcType.Int).Value = 0;
    cmd.Parameters.Add("@FailedPasswordAnswerAttemptWindowStart", OdbcType.DateTime).Value = createDate;

    try
    {
      conn.Open();

      int recAdded = cmd.ExecuteNonQuery();

      if (recAdded > 0)
      {
        status = MembershipCreateStatus.Success;
      }
      else
      {
        status = MembershipCreateStatus.UserRejected;
      }
    }
    catch (OdbcException)
    {
      // Handle exception.

      status = MembershipCreateStatus.ProviderError;
    }
    finally
    {
      conn.Close();
    }


    return GetUser(username, false);
  }
  else
  {
    status = MembershipCreateStatus.DuplicateUserName;
  }


  return null;
}


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4

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.
Show: