2013-10-11 100 views
0

這是我第一次運行任何類型的查詢和/或通過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 

我該如何使用類似的東西來更改密碼?

+6

你不想使用'LIKE'使用密碼。請使用完全匹配('=')。 – pickypg

+0

@pickypg謝謝,已將其更改爲我的文件。 –

回答

2

由於@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