2012-03-19 36 views
5

Sto utilizzando l'API Membership ASP.NET. Voglio forzare l'utente a cambiare la sua password dopo la prima volta che effettua il login. Ma non sono riuscito a trovare alcuna funzionalità incorporata nell'API Membership di ASP.NET.
È possibile o no? Se sì, come può essere fatto facilmente?L'API di iscrizione ASP.NET modifica la password della password

+1

http://forums.asp.net/p/1273575/2414481.aspx –

risposta

8

Ecco a voi, una soluzione completamente testata;)

protected void LoginButton_Click(object sender, EventArgs e) 
{ 
    /****note: UserName and Password are textbox fields****/ 

    if (Membership.ValidateUser(UserName.Text, Password.Text)) 
    { 
     MembershipUser user = Membership.GetUser(UserName.Text); 
     if (user == null) 
     { 
      FailureText.Text = "Invalid username. Please try again."; 
      return; 
     } 
     if (user.IsLockedOut) 
      user.UnlockUser(); 

     /* this is the interesting part for you */ 
     if (user.LastPasswordChangedDate == user.CreationDate) //if true, that means user never changed their password before 
     { 
      //TODO: add your change password logic here 
     } 
    } 
} 

Nel caso abbiate bisogno di aiuto su come modificare la password, per favore fatemelo sapere.

Qualora questo post essere in qualsiasi aiuto per voi, si prega di contrassegnare come risposta

+1

In alternativa alla proprietà LastPasswordChangedDate, è anche possibile utilizzare la proprietà Commento (che è solo una stringa a mano libera) per indicare che gli utenti hanno bisogno di resettare le loro password, nel caso in cui la logica della password di modifica non giri sempre attorno alle date di modifica della password. –

0

seguito è la soluzione in VB. Include anche FindControl per la lettura e l'impostazione degli elementi del modulo asp:Login ID="LoginUser".

Protected Sub LoginButton_Click(sender As Object, e As EventArgs) 

    '***note: UserName and Password are textbox fields*** 
    Dim UserName As TextBox = DirectCast(LoginUser.FindControl("UserName"), TextBox) 
    Dim Password As TextBox = DirectCast(LoginUser.FindControl("Password"), TextBox) 
    Dim FailureText As Literal = DirectCast(LoginUser.FindControl("FailureText"), Literal) 

    If Membership.ValidateUser(UserName.Text, Password.Text) Then 
     Dim user As MembershipUser = Membership.GetUser(UserName.Text) 
     If user Is Nothing Then 
      FailureText.Text = "Invalid username. Please try again." 
      Return 
     End If 
     If user.IsLockedOut Then 
      user.UnlockUser() 
     End If 

     ' this is the interesting part for you 

     If user.LastPasswordChangedDate = user.CreationDate Then 
      'TODO: add your change password logic here 
     End If 
    End If 
End Sub 
0

Ecco come l'ho fatto. E 'meglio farlo dopo l'accesso.

protected void LoginUser_LoggedIn(object sender, EventArgs e) 
    { 

     if (Membership.ValidateUser(this.LoginUser.UserName, this.LoginUser.Password)) 
     { 
      MembershipUser user = Membership.GetUser(this.LoginUser.UserName); 
      if (user == null) 
      { 
       this.LoginUser.FailureText = "Invalid username. Please try again."; 
       return; 
      } 
      if (user.IsLockedOut) 
      { 
       user.UnlockUser(); 
      } 

      if (user.LastPasswordChangedDate == user.CreationDate) //if true, that means user never changed their password before 
      { 
       Response.Redirect("~/Account/ChangePassword.aspx"); 
      } 
     } 
    } 
Problemi correlati