Bạn đang ghi đè sai phương pháp, Steve. Bạn muốn ghi đè số bị hủy ChangingPassword
.
Hãy thử điều này:
protected void ChangePassword1_ChangingPassword(object sender, LoginCancelEventArgs e)
{
// do your lookup here,
bool passwordHasBeenPreviouslyUsed = true;
if (passwordHasBeenPreviouslyUsed)
{
e.Cancel = true;
// notify of error
return;
}
}
Và, như mỗi buổi Q/A trước, Bạn nên NEVER EVER BAO GIỜ cửa hàng mật khẩu của người dùng . Đi đến bảng thành viên và nhận được muối và sử dụng nó để băm mật khẩu đến để so sánh với các giá trị đã băm đã được lưu trữ trong bảng tra cứu của bạn.
Chúc may mắn.
(1) - vị trí của bạn sẽ ở mức nào khi Giám đốc điều hành phát hiện ra rằng mật khẩu của anh ta đã được lưu trữ ở định dạng có thể khai thác? Có một mức độ tin tưởng dành cho các pháp sư da đen là chúng tôi và niềm tin đó mang những rủi ro của chính nó. Hãy nhận biết về chúng. ;-)
EDIT:
Một ví dụ làm việc:
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>
Cập nhật: Bạn cũng có thể quan tâm đến việc chỉ đơn giản là việc xác định một handler trong một phạm vi cao hơn sẽ xem tất cả hoạt động của mật khẩu:
xem xét điều này
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");
}
Tôi không chắc bạn đang cố gắng làm gì ở đây. Bạn nói về việc muốn xác minh mật khẩu cũ, nhưng câu lệnh SQL của bạn truy xuất tên người dùng và không bao giờ kiểm tra mật khẩu. Bạn tự xác minh mật khẩu bằng cách nào? – cortijon
Có, bạn chính xác, đoạn mã đó nằm trên một trang khác, tôi đã thực sự nhanh chóng kết hợp một trang thử nghiệm, xin lỗi về sự nhầm lẫn. Tôi chỉ gặp rắc rối khi làm gián đoạn quá trình. Cảm ơn bạn đã nhập. – Steve
Bất kỳ tiến bộ nào về điều này? –