vb.net 如何使多用户登录页面代码更简单?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22451723/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 17:06:58  来源:igfitidea点击:

How to make login page code with multiple users simpler?

vb.net

提问by Jamie

In VB.NET project, I'm trying to create a login page which users could login using a form. I used an array to save the login information. However, it's complicated when verifying, and thus I cannot add a user simply by adding item to array. I'll need to add a else ifin the code, so it's very inconvenient.

在 VB.NET 项目中,我试图创建一个用户可以使用表单登录的登录页面。我使用了一个数组来保存登录信息。但是,验证时很复杂,因此我不能简单地通过将项目添加到数组来添加用户。我需要else if在代码中添加一个,所以很不方便。

The code is below :

代码如下:

Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
    Dim UserNameAvailable As Integer = 0
    Dim UsernameList(2) As String
    Dim PasswordList(2) As String
    UsernameList(0) = "jamiechoi"
    PasswordList(0) = "198237645"
    UsernameList(1) = "marcoyeung"
    PasswordList(1) = "infotalkong"
    UsernameList(2) = "user"
    PasswordList(2) = "toolbox"
    Dim InputUsername As String
    InputUsername = UsernameTextBox.Text
    Dim InputPassword As String
    InputPassword = PasswordTextBox.Text
    If InputUsername = UsernameList(0) Then
        If InputPassword = PasswordList(0) Then
            Me.Hide()
            AdminArea.Show()
            UserNameAvailable = 1
        Else
            MsgBox("Wrong Password!")
            UserNameAvailable = 1
        End If
    ElseIf InputUsername = UsernameList(1) Then
        If InputPassword = PasswordList(1) Then
            Me.Hide()
            AdminArea.Show()
            UserNameAvailable = 1
        Else
            MsgBox("Wrong Password!")
            UserNameAvailable = 1
        End If
    ElseIf InputUsername = UsernameList(2) Then
        If InputPassword = PasswordList(2) Then
            Me.Hide()
            AdminArea.Show()
            UserNameAvailable = 1
        Else
            MsgBox("Wrong Password!")
            UserNameAvailable = 1
        End If
    End If

    If UserNameAvailable = 0 Then
        MsgBox("Wrong Username!")
    End If
End Sub

I hope that I could use a forloop to complete the verifying task. What can I do?

我希望我可以使用一个for循环来完成验证任务。我能做什么?

回答by sloth

I guess this is just a programming exercise and not a real-world application (at least I hope so) so I won't get deep into the whole never-hardcode-secret-informations-in-your-code-thing.

我想这只是一个编程练习,而不是一个现实世界的应用程序(至少我希望如此)所以我不会深入研究整个从不硬编码秘密信息在你的代码中的事情。



Usernames and passwords have a 1-to-1 relationship (each username has one password), so it's sensible to express that in code.

用户名和密码是一对一的关系(每个用户名有一个密码),所以用代码来表达是明智的。

The easiest way is to use a Dictionaryto create a mapping username->password. Then we can use TryGetValueto check if the username exists and retrieve the password:

最简单的方法是使用 aDictionary创建映射 username->password。然后我们可以使用TryGetValue检查用户名是否存在并检索密码:

Private Sub OK_Click(sender As System.Object, e As System.EventArgs) Handles OK.Click
    Dim credentials = New Dictionary(Of String, String) From _
    {
        {"jamiechoi", "198237645"},
        {"marcoyeung", "infotalkong"},
        {"user", "toolbox"}
    }

    Dim passwd As String
    If credentials.TryGetValue(UsernameTextBox.Text, passwd) AndAlso passwd = PasswordTextBox.Text Then
        Me.Hide()
        AdminArea.Show()
    Else
        MsgBox("Wrong User/Password!")
    End If

End Sub

Note that in a real-world-application you would not store the usernames/passwords in code, but in e.g. a database, and also you would just store a salted hash of the password, not the plaintext.

请注意,在现实世界的应用程序中,您不会将用户名/密码存储在代码中,而是存储在例如数据库中,而且您只会存储密码的加盐哈希,而不是明文。

回答by Jamie

Sorry for answering so late (and didn't accept the answer) because the result of answer was unexpected.

很抱歉这么晚才回答(并且没有接受答案),因为答案的结果出乎意料。

This is my expected solution :

这是我预期的解决方案:

(Something like)

(就像是)

For i As Integer = 1 To UsernameList.Length
    If InputUsername = UsernameList(i) AndAlso InputPassword = PasswordList(i) Then
        Me.Hide()
        AdminArea.Show()
    End If
Next

And that's my solution.

这就是我的解决方案。

回答by Dylan666

here is my little correction to the code:

这是我对代码的小修正:

For i As Integer = 0 To UsernameList.Length - 1
    If InputUsername = UsernameList(i) AndAlso InputPassword = PasswordList(i) Then
        Me.Hide()
        AdminArea.Show()
    End If
Next