2012-03-19 108 views
5

我正在使用ASP.NET Membership API。我想強制用戶在第一次登錄後更改密碼。但是,我無法在ASP.NET的Membership API中找到任何此類內置功能。
是否有可能?如果是的話,怎麼能輕鬆完成呢?ASP.NET會員API強制密碼更改

+1

http://forums.asp.net/p/1273575/2414481.aspx – 2012-03-19 13:07:54

回答

8

給你,完全測試的解決方案;)

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 
     } 
    } 
} 

如果您需要在如何修改密碼幫助,請讓我知道。

這篇文章對你有幫助嗎,請標記爲答案

+1

至於LastPasswordChangedDate財產的替代方案,也可以使用Comment屬性(這只是一個自由格式的字符串)來指示用戶需要重置他們的密碼,以防您的更改密碼邏輯並不總是圍繞密碼更改日期。 – 2013-07-31 02:24:04

0

下面是VB中的解決方案。它還包括用於讀取和設置asp:Login ID="LoginUser"表單元素的FindControl。

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

這就是我做到的。最好是在登錄後做到這一點。

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"); 
      } 
     } 
    }