如果用户名和旧密码匹配,请更新密码

时间:2013-10-11 01:20:24

标签: sql-server vb.net

这是我第一次运行任何类型的查询和/或通过vb连接到数据库。我已经在网上查找了我的问题,但还没找到我正在寻找的东西。

我的Windows应用程序上有一个简单的登录页面,它运行在一个紧凑的.sdf数据库中。我需要添加一个允许用户更改密码的过程。

如果textbox1中的用户名和textbox2中的密码与我在数据库中存储的密码相匹配,则将密码替换为textbox3的值。

到目前为止,我已经能够弄清楚如何创建一个新帐户并验证登录。我使用以下命令登录:

SELECT        username, userpassword
FROM            UserInfo
WHERE        (username LIKE @username) AND (userpassword LIKE @userpassword)

然后我的按钮上的程序:

' Check if username or password is empty
If txtPassword.Text = "" Or txtUserName.Text = "" Then
    MessageBox.Show("Please complete the required fields..", "Authentication Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

    'Clear all fields
    txtPassword.Text = ""
    txtUserName.Text = ""

    'Focus on Username field
    txtUserName.Focus()

Else

    'If the password and username match, open the main form.
    If Not UserInfoTableAdapter1.Login(txtUserName.Text, txtPassword.Text) = Nothing Then

        Dim frmWelcome As New frmWelcomePage

        frmWelcome.Show()
        Me.Hide()

    Else


        MessageBox.Show("You have entered an invalid user name or password", "Invalid Login", MessageBoxButtons.OK, MessageBoxIcon.Error)

        'Clear all fields
        txtPassword.Text = ""
        txtUserName.Text = ""

        'Focus on Username field
        txtUserName.Focus()

    End If

End If

如何使用类似的内容更改密码?

1 个答案:

答案 0 :(得分:3)

正如@pickypg所说,你绝对应该寻找与密码和用户名的完全匹配。您还应该考虑用户密码的单向哈希。 This answer 可以很好地描述存储纯文本密码的潜在危险。 This article 有相关信息,也很有趣。

除了您正在寻找的sql可能是这样的:

create procedure updateUserPassword
 @userName varchar(max)
,@oldHashedPassword nvarchar(128)
,@newHashedPassword nvarchar(128)
as
begin
set nocount on;
  if exists ( select 1 from UserInfo where username = @userName and userpassword = @oldHashedPassword )
  begin
    update UserInfo set userPassword = @newHashedPassword where username = @userName;
  end
  else
  begin
   raiserror('No record found for user', 16, 1);
  end
end
相关问题