Partager via


ValidatePasswordEventArgs(String, String, Boolean) Constructeur

Définition

Crée une instance de la classe ValidatePasswordEventArgs.

public:
 ValidatePasswordEventArgs(System::String ^ userName, System::String ^ password, bool isNewUser);
public ValidatePasswordEventArgs (string userName, string password, bool isNewUser);
new System.Web.Security.ValidatePasswordEventArgs : string * string * bool -> System.Web.Security.ValidatePasswordEventArgs
Public Sub New (userName As String, password As String, isNewUser As Boolean)

Paramètres

userName
String

Nom de l’utilisateur d’appartenance pour l’action create-user, change-password ou reset-password actuelle.

password
String

Nouveau mot de passe pour l’utilisateur d’appartenance spécifié.

isNewUser
Boolean

true si l’événement se produit pendant la création d’un utilisateur ; sinon, false.

Exemples

L’exemple de code suivant montre un exemple d’implémentation ChangePassword qui crée un objet ValidatePasswordEventArgs à passer à l’événement ValidatingPassword .

public override bool ChangePassword(string username, string oldPwd, string newPwd)
{
  if (!ValidateUser(username, oldPwd))
  {
    return false;
  }

  ValidatePasswordEventArgs args =
    new ValidatePasswordEventArgs(username, newPwd, true);

  OnValidatingPassword(args);

  if (args.Cancel)
    if (args.FailureInformation != null)
      throw args.FailureInformation;
    else
      throw new MembershipPasswordException("Change password canceled due to new password validation failure.");

  OdbcConnection conn = new OdbcConnection(ConnectionString);
  OdbcCommand cmd = new OdbcCommand("UPDATE Users " +
            " SET Password = ?, LastPasswordChangedDate = ? " +
            " WHERE Username = ? AND Password = ? AND ApplicationName = ?", conn);

  cmd.Parameters.Add("@Password", OdbcType.VarChar, 128).Value = EncodePassword(newPwd);
  cmd.Parameters.Add("@LastPasswordChangedDate", OdbcType.DateTime).Value = DateTime.Now;
  cmd.Parameters.Add("@Username", OdbcType.VarChar, 255).Value = username;
  cmd.Parameters.Add("@OldPassword", OdbcType.VarChar, 128).Value = oldPwd;
  cmd.Parameters.Add("@ApplicationName", OdbcType.VarChar, 255).Value = ApplicationName;

  int rowsAffected = 0;

  try
  {
    conn.Open();

    rowsAffected = cmd.ExecuteNonQuery();
  }
  catch (OdbcException)
  {
    // Handle exception.
  }
  finally
  {
    conn.Close();
  }

  if (rowsAffected > 0)
  {
    return true;
  }

  return false;
}
Public Overrides Function ChangePassword(username As String, _
                                         oldPwd As String, _
                                         newPwd As String) As Boolean
  
  If Not ValidateUser(username, oldPwd) Then
    Return False
  End If

  Dim args As ValidatePasswordEventArgs = _
    New ValidatePasswordEventArgs(username, newPwd, True)

  OnValidatingPassword(args)
  
  If args.Cancel Then
    If Not args.FailureInformation Is Nothing Then
      Throw args.FailureInformation
    Else
      Throw New MembershipPasswordException("Change password canceled due to New password validation failure.")
    End If
  End If


  Dim conn As OdbcConnection = New OdbcConnection(ConnectionString)
            Dim cmd As OdbcCommand = New OdbcCommand("UPDATE Users " & _
                       " SET Password = ?, LastPasswordChangedDate = ? " & _
                       " WHERE Username = ? AND Password = ? AND ApplicationName = ?", conn)

  cmd.Parameters.Add("@Password", OdbcType.VarChar, 128).Value = EncodePassword(newPwd)
  cmd.Parameters.Add("@LastPasswordChangedDate", OdbcType.DateTime).Value = DateTime.Now
  cmd.Parameters.Add("@Username", OdbcType.VarChar, 255).Value = username
  cmd.Parameters.Add("@OldPassword", OdbcType.VarChar, 128).Value = oldPwd
  cmd.Parameters.Add("@ApplicationName", OdbcType.VarChar, 255).Value = ApplicationName


  Dim rowsAffected As Integer = 0

  Try
    conn.Open()

    rowsAffected = cmd.ExecuteNonQuery()
  Catch e As OdbcException
    ' Handle exception.
  Finally
    conn.Close()
  End Try

  If rowsAffected > 0 Then Return True

  Return False
End Function

Remarques

Le ValidatePasswordEventArgs constructeur est utilisé par une implémentation de fournisseur d’appartenances dans les CreateUserimplémentations de méthode , ChangePasswordet ResetPassword .

S’applique à

Voir aussi