2010-04-27 25 views
5

Ho una nuova tabella che contiene vecchie password, ho bisogno di verificare se c'è una corrispondenza.Controllo appartenenza ASP.NET Cambia password - È necessario verificare la password precedente

Se è presente una corrispondenza, è necessario che il comando ChangePassword NON cambi la password. Devo dire all'utente che questa password è stata utilizzata e fotografarne una nuova.

Non riesco a essere in grado di interrompere il controllo dalla modifica della password. Forse sto usando l'evento sbagliato.

Ecco un pezzo del mio codice o come vorrei che funzionasse. Apprezzo tutto il tuo aiuto.

protected void ChangePassword1_ChangedPassword(object sender, EventArgs e) 
    { 
     MembershipUser user = Membership.GetUser(); 
     string usrName = ""; 
     if (user != null) 
     { 
      string connStr = ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString; 
      SqlConnection mySqlConnection = new SqlConnection(connStr); 
      SqlCommand mySqlCommand = mySqlConnection.CreateCommand(); 
      mySqlCommand.CommandText = "Select UserName from OldPasswords where UserName = 'test'"; 
      mySqlConnection.Open(); 
      SqlDataReader mySqlDataReader = mySqlCommand.ExecuteReader(CommandBehavior.Default); 
      while (mySqlDataReader.Read()) 
      { 
       usrName = mySqlDataReader["UserName"].ToString(); 
       if (usrName == user.ToString()) 
       { 

        Label1.Text = "Match"; 
       } 
       else 
       { 
        Label1.Text = "NO Match!"; 
       } 
      } 
+1

Non sono sicuro di cosa stai provando a fare qui. Si parla di voler verificare le vecchie password, ma l'istruzione SQL recupera nomi utente e non verifica mai le password. Come stai verificando la password stessa? – cortijon

+0

Sì, hai ragione, quel pezzo di codice si trova su una pagina diversa, sono stato davvero veloce a mettere insieme una pagina di test, mi dispiace per la confusione. Ho appena avuto problemi a interrompere il processo stesso. Grazie per l'input. – Steve

+0

Qualche progresso su questo? –

risposta

6

Stai ignorando il metodo sbagliato, Steve. Si desidera ignorare il cancellabile ChangingPassword.

provare questo:

protected void ChangePassword1_ChangingPassword(object sender, LoginCancelEventArgs e) 
{ 
    // do your lookup here, 
    bool passwordHasBeenPreviouslyUsed = true; 

    if (passwordHasBeenPreviouslyUsed) 
    { 
     e.Cancel = true; 
     // notify of error 
     return; 
    } 

} 

E, come da precedenti sessioni di Q/A, non si dovrebbe mai MAI MAI memorizzazione delle password di un utente . Vai alla tabella dei membri e prendi il sale e usalo per cancellare la password in entrata per confrontarla con i valori già saltati che hai salvato nella tua tabella di ricerca.

Buona fortuna.

(1) - quanto è probabile la tua posizione quando il CEO scopre che la sua password è stata archiviata in un formato sfruttabile? C'è un livello di fiducia dato ai maghi neri che siamo noi e che la fiducia comporta i suoi rischi. Sii consapevole di loro. ;-)

EDIT:

Un esempio di lavoro:

ChangePassword.aspx

<%@ Page Language="C#" %> 
<%@ Import Namespace="System.Diagnostics"%> 

<script runat="server"> 
    protected void Page_Load(object sender, EventArgs e) 
    { 

    } 

    protected void ChangePassword1_ChangingPassword(object sender, LoginCancelEventArgs e) 
    { 
     // works for me! 
     Debugger.Break(); 
    } 
</script> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
     <asp:ChangePassword ID="ChangePassword1" runat="server" OnChangingPassword="ChangePassword1_ChangingPassword"> 
     </asp:ChangePassword> 
    </div> 
    </form> 
</body> 
</html> 

Aggiornamento: Potreste anche essere interessati a definire semplicemente un gestore in un ambito più alto che guarderà tutte le attività della password:

considera questo

public void SetupPasswordActionHook() 
{ 

    //Occurs when a user is created, a password is changed, or a password is reset. 
    Membership.ValidatingPassword += Membership_ValidatingPassword; 
} 

void Membership_ValidatingPassword(object sender, ValidatePasswordEventArgs e) 
{ 

    // Gets a value that indicates whether the System.Web.Security.MembershipProvider.ValidatingPassword event is being raised during a 
    // call to the System.Web.Security.MembershipProvider.CreateUser() method. 

    // true if the System.Web.Security.MembershipProvider.ValidatingPassword event is being raised during a call to the 
    // System.Web.Security.MembershipProvider.CreateUser() method; otherwise, false. 
    bool isNewUser = e.IsNewUser; 

    // Gets the password for the current create-user, change-password, or reset-password action. 

    // The password for the current create-user, change-password, or reset-password action. 
    string password = e.Password; 

    // Gets the name of the membership user for the current create-user, change-password, or reset-password action. 

    // The name of the membership user for the current create-user, change-password, or reset-password action. 
    string username = e.UserName; 

    // Gets or sets a value that indicates whether the current create-user, change-password, or reset-password action will be canceled. 

    // true if the current create-user, change-password, or reset-password action will be canceled; otherwise, false. The default is false. 
    e.Cancel = true; 

    // Gets or sets an exception that describes the reason for the password-validation failure. 

    // An System.Exception that describes the reason for the password-validation failure. 
    e.FailureInformation = new Exception("This is why I failed your password"); 

} 
+0

Grazie mille per il tuo esempio di codice, ci proverò. Ovviamente NON sto salvando nulla in formato chiaro, la password è hashhed in entrambe le tabelle e conservo il sale con esso. Ho la password che l'utente inserisce e quindi la confronta con quella che ho nella mia tabella "OldPassword". Ancora una volta, grazie per il vostro aiuto. – Steve

+0

@Sky - Sai perché questo non si attiva nel mio codice? – Steve

+0

@Steve - (mettendo su un cappello psichico) hmm .... Sento che potresti non aver specificato sul controllo che l'evento ChangingPasswordHandler è ChangePassword1_ChangingPassword. (togliendo il cappello psichico) Come ho fatto? ;-) –

Problemi correlati