vb.net - 如何使 Vb.Net 登录表单允许多个用户使用存储在 .DAT 文件中的密码登录
问题描述
我有一个用 VB 2008 设计的登录表单。用户可以通过从组合框中的下拉列表中选择管理员或访客来登录。然后输入一个密码,该密码必须与存储在 .DAT 文件中的相应用户名的密码匹配。 显示存储在 .DAT 文件中的用户名和密码的屏幕截图
文件上的第二个用户名 Guest 和密码“passwordG”正在工作,而第一个用户名 Admin 和密码“passwordG”不起作用。
Public Class frmlogin
Dim user_name As String
Dim pass_word As String
Dim counter As Byte
Dim attempts As Integer = 0
Private Sub login_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
cmbUsername.Items.Add("Admin")
cmbUsername.Items.Add("Guest")
FileOpen(1, My.Application.Info.DirectoryPath & "\users.DAT", OpenMode.Input)
Do Until EOF(1)
Input(1, user_name)
Input(1, pass_word)
Loop
FileClose(1)
请我需要有关如何使这项工作的帮助。那如何使程序接受密码。代码是否不正确或不充分或密码未正确存储在 .DAT 文件中?拜托我需要你的帮忙。谢谢你。
解决方案
只需使用纯文本文件。否则,我们将不得不去掉引号。
Admin,passwordA
Guest,passwordG
由于我关闭了登录表单,请确保关闭的应用程序属性设置如下:
Private GuestPassword As String
Private AdminPassword As String
Private Sub login_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Fill the combo box
cmbUsername.Items.Add("Admin")
cmbUsername.Items.Add("Guest")
'one of the .net methods to read a file, returns an array of lines in the file
Dim lines = File.ReadAllLines("user.txt")
'Split by the comma, the first line into and array
Dim splits = lines(0).Split(","c)
'The second element of this array holds the Admin password
AdminPassword = splits(1)
splits = lines(1).Split(","c)
GuestPassword = splits(1)
End Sub
Private Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click
If cmbUserName.Text = "Admin" AndAlso AdminPassword = txtPassword.Text Then
'Show the Admin form
ElseIf cmbUserName.Text = "Guest" AndAlso GuestPassword = txtPassword.Text Then
'Show the guest form
Else
MessageBox.Show("Sorry, no match")
End If
Me.Close()
End Sub
编辑
我们将使用 aDictionary
将用户存储在内存中。这提供了非常快速的查找。
字典包含键/值对。请注意,在表单加载中,我们将Dictionary
密码填写为Key
,用户类型填写为Value
.
对空格要非常小心。文本文件或文本框中不应有任何内容。您可能需要添加.Trim
以作为预防措施。
在按钮单击中,我们userType
通过搜索用户提供的密钥来检索。这被一个Try
块包围,因为如果找不到密钥,则会引发异常。
Private UsersDictionary As New Dictionary(Of String, String)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim lines = File.ReadAllLines("users.txt")
For Each line In lines
Dim splits = line.Split(","c)
UsersDictionary.Add(splits(1), splits(0))
Next
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim userType As String
Try
userType = UsersDictionary.Item(TextBox1.Text)
Catch ex As System.Collections.Generic.KeyNotFoundException
MessageBox.Show("Sorry, you are not a registered user.")
Exit Sub
End Try
If userType = "Admin" Then
'open the admin form
Else
'open the user form
End If
Me.Close()
End Sub
推荐阅读
- javascript - Nw.js program with tileserver-gl
- graphql - Redwood JS: Cannot query field XX on type "Query"
- android - Flutter ARcore如何以3D显示小部件?
- javascript - 我正在寻找一种方法来检查它是否在 JS 中的 Fetch 中正确获得响应
- java - 如何通过Java设置Z3的并行模式
- testcontainers - 使用“withFileSystemBind”绑定卷 -> 权限被拒绝
- android - Android - 加入路径并删除相交部分
- r - 在 dplyr case_when 中输入所有列名并根据条件分配给新列
- r - 如何在R中将一个小的df插入一个更大的df
- sql-server - 从 SSC SQL Server 数据库中检索代码片段和中低发现