2012-03-19 44 views
5

Tôi đang sử dụng API thành viên ASP.NET. Tôi muốn ép buộc người dùng thay đổi mật khẩu của họ sau lần đầu tiên anh ta đăng nhập. Nhưng, tôi không thể tìm thấy bất kỳ chức năng nào được tích hợp trong API thành viên của ASP.NET.
Có thể, hay không? Nếu có, làm thế nào nó có thể được thực hiện dễ dàng?Thay đổi mật khẩu API thành viên ASP.NET

+1

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

Trả lời

8

đây bạn đang có, một giải pháp kiểm tra đầy đủ;)

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

Trong trường hợp bạn cần sự giúp đỡ trong làm thế nào để thay đổi mật khẩu, xin vui lòng cho tôi biết.

Nếu bài đăng này có bất kỳ trợ giúp nào cho bạn, vui lòng gắn thẻ làm câu trả lời

+1

Để thay thế cho tài sản LastPasswordChangedDate, bạn cũng có thể sử dụng tài sản Comment (đó chỉ là chuỗi dạng tự do) để cho biết người dùng cần đặt lại mật khẩu của họ, trong trường hợp logic thay đổi mật khẩu của bạn không phải lúc nào cũng xoay quanh ngày thay đổi mật khẩu. –

0

Dưới đây là giải pháp trong VB. Nó cũng bao gồm FindControl để đọc và thiết lập các phần tử biểu mẫu 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

Đây là cách tôi đã làm. Nó là tốt hơn để làm điều đó sau khi đăng nhập.

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"); 
      } 
     } 
    } 
Các vấn đề liên quan