首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >我希望使用文本文件来验证用户名和密码登录

我希望使用文本文件来验证用户名和密码登录
EN

Stack Overflow用户
提问于 2019-03-19 19:30:41
回答 1查看 55关注 0票数 0

我希望在VB.NET中使用文本文件来验证用户名和密码。

我已经验证了用户名,但我无法验证密码,在txtpassowrd.text中输入的任何内容都将导致登录。

我使用的代码是:

代码语言:javascript
运行
复制
Imports System.IO
Public Class frmReceptionist
    Function IsInFile(ByVal person As String) As Boolean
        If File.Exists("receptionistUser.txt") And File.Exists("receptionistPassword.txt") Then

            Dim sr As StreamReader = File.OpenText("receptionistUser.txt")
            Dim individual As String

            Do Until sr.EndOfStream
                individual = sr.ReadLine

                If individual = person Then
                    sr.Close()
                    Return True
                End If

            Loop
            sr.Close()
        End If
        Return False

    End Function

    Private Sub btnConfirm_Click(sender As Object, e As EventArgs) Handles btnConfirm.Click
        'Determine if a person is in the file
        Dim person As String = txtUsername.Text

        If person <> "" Then
            If IsInFile(person) Then

                MessageBox.Show(person & " Welcome Receptionist", "Bia Duitse")
                Me.Hide()
                frmBiaDuitse.Show()

            Else
                MessageBox.Show(person & " Incorrect Login", "No")

            End If
        Else
            MessageBox.Show("You must enter Details", "Information")
        End If
        txtUsername.Clear()
        txtUsername.Focus()

    End Sub
    Private Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click
        Me.Hide()
        frmSelectJob.Show()

    End Sub

End Class
EN

Stack Overflow用户

发布于 2019-03-19 20:58:20

这绝对不是你应该这样做的方式。

出于学习目的,您可以将文件加载到Dictionary()中,如下所示:

代码语言:javascript
运行
复制
Private Credentials As Dictionary(Of String, String)

Private Sub LoadCredentials()
    If IsNothing(Credentials) Then
        Credentials = New Dictionary(Of String, String)()
        If File.Exists("receptionistUser.txt") And File.Exists("receptionistPassword.txt") Then
            Dim users() As String = File.ReadAllLines("receptionistUser.txt")
            Dim passwords() As String = File.ReadAllLines("receptionistPassword.txt")
            If users.Length = passwords.Length Then
                For i As Integer = 0 To users.Length - 1
                    Credentials.Add(users(i), passwords(i))
                Next
            End If
        End If
    End If
End Sub

Function IsInFile(ByVal person As String) As Boolean
    LoadCredentials()
    If Not IsNothing(Credentials) Then
        Return Credentials.ContainsKey(person)
    End If
    Return False
End Function

Function Checkpassword(ByVal person As String, ByVal password As String) As Boolean
    LoadCredentials()
    If Not IsNothing(Credentials) Then
        Return Credentials.ContainsKey(person) AndAlso password = Credentials(person)
    End If
    Return False
End Function
票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55240015

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档