ASP.NET Membership ChangePassword 控件 - 需要检查以前的密码
我有一个保存旧密码的新表,我需要检查是否有匹配项。
如果存在匹配项,我需要 ChangePassword 控件来不更改密码。我需要告诉用户该密码已被使用,并设置一个新密码。
我似乎无法中断更改密码的控制。 也许我使用了错误的事件。
这是我的一段代码,或者我希望它如何工作。 我感谢你的所有帮助。
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!";
}
}
I have a new table that hold old passwords, I need to check if there is a match.
If there is a match I need the ChangePassword contol to NOT change the password. I need to tell the user that this password was used and pic a new one.
I can't seem to be able to interrupt the control from changing the password.
Maybe I am using the wrong event.
Here is a piece of my code, or how I wish it would work.
I appreciate all your help.
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!";
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你重写了错误的方法,史蒂夫。您想要覆盖可取消的
ChangingPassword
。试试这个:
并且,根据之前的问答环节,您永远不应该永远存储用户的密码1。转到成员资格表并获取盐,并使用它对传入密码进行哈希处理,以与您存储在查找表中的已经过盐哈希处理的值进行比较。
祝你好运。
(1) - 当首席执行官发现他的密码以可利用的格式存储时,您的立场有多站得住脚?我们对黑法师有一定程度的信任,而这种信任本身就有风险。注意他们。 ;-)
编辑:
一个工作示例:
ChangePassword.aspx
更新:
您可能还对简单地在更高范围内定义一个处理程序来监视所有密码活动感兴趣:
考虑一下
You are overriding the wrong method, Steve. You want to override the cancellable
ChangingPassword
.Try this:
And, as per previous Q/A sessions, You should NEVER EVER EVER store a user's password1. Go to the membership table and get the salt and use that to hash the incoming password to compare to the already salt-hashed values you have stored in your lookup table.
Good luck.
(1) - how tenable would your position be when the CEO finds out that his password has been stored in an exploitable format? There is a level of trust given to the black mages that are us and that trust carries it's own risks. Be aware of them. ;-)
EDIT:
A working example:
ChangePassword.aspx
Update:
You may also be interested in simply defining a handler in a higher scope that will watch all password activity:
consider this