none
Authentification RRS feed

  • Question

  • How do I Make this so the participant is able to re-enter the password and/or the username if it is incorrect? Also how do I reset the file reader to the beginning so it doesn't crash when they try to enter there details more than the amount of lines in the file.

    Here is my code:

    Imports System.IO
    Module Module1
        Dim Password As String
        Dim Passwordexists As String
        Dim Username As String
        Dim Usernameexists As String
        Dim LoginYN As String
        Dim Correct As Boolean = False
        Sub Main()
            While Correct = False
                If IO.File.Exists("ENTER FILE HERE") Then
                    Console.WriteLine("Have you created a login? Enter Yes or No.")
                    LoginYN = Console.ReadLine
                    If LoginYN = "Yes" Then
                        Console.WriteLine("Please enter Your username")
                        Usernameexists = Console.ReadLine
                        FileOpen(1, "ENTER FILE HERE", OpenMode.Input)
                        While Not EOF(1)
                            If LineInput(1) = Usernameexists Then
                                Console.WriteLine("Please enter your password")
                                Passwordexists = Console.ReadLine
                            End If
                            If LineInput(1) = Passwordexists Then
                                Console.WriteLine("Password Correct press enter to continue.")
                                Console.ReadLine()
                            End If
                        End While
                        FileClose(1)
                        Correct = True
                    ElseIf LoginYN = "No" Then
                        FileOpen(1, "ENTER FILE HERE", OpenMode.Append)
                        Console.WriteLine("Please enter a Username")
                        Username = Console.ReadLine
                        PrintLine(1, Username)
                        Console.WriteLine("Please enter a Password")
                        Password = Console.ReadLine
                        PrintLine(1, Password)
                        FileClose(1)
                    End If
                Else
                    Console.WriteLine("Error file not found, Please try again")
                End If
            End While
            Console.WriteLine("Done")
            Console.ReadLine()
        End Sub
    End Module

     
    Tuesday, October 15, 2019 5:23 PM

All replies

  • Hello,

    • You should not be asking for a file name, it should be known to the program
    • Also, to get started work with a local function (seen in figure 1)
    • Forget out using VB6 style of coding
    • Use a class for user information
    • For a file for learning only see figure 2 as in real life nobody stored user name/password in plain text.
    Module Module1
    
        Sub Main()
            Console.ForegroundColor = ConsoleColor.Cyan
    
            Console.WriteLine("Login")
            Console.ForegroundColor = ConsoleColor.White
    
            Dim userLoginName = ""
            Dim loginAttempts As Integer = 0
    
            'Simple iteration upto three times
            For index As Integer = 0 To 2
                Console.Clear()
                Console.Write("Enter username: ")
                Dim username As String = Console.ReadLine()
                Console.Write("Enter password: ")
                Dim password As String = Console.ReadLine()
    
                Dim foundUser = Users.FirstOrDefault(
                    Function(user) user.Name = username AndAlso user.Password = password)
    
                If foundUser Is Nothing Then
                    loginAttempts += 1
                Else
                    userLoginName = username
                    Exit For
                End If
            Next index
    
            Console.Clear()
    
            'Display the result
            If loginAttempts > 2 Then
                Console.WriteLine("Login failure")
            Else
                Console.WriteLine($"Login successful for {userLoginName}")
            End If
    
    
            Console.ReadKey()
    
        End Sub
    
    
        Public Function Users() As List(Of User)
            Return New List(Of User) From
                {
                    New User() With {.Name = "jimbeam", .Password = "p1"},
                    New User() With {.Name = "kp", .Password = "payne"
                }}
        End Function
    
    End Module
    Public Class User
        Public Property Name() As String
        Public Property Password() As String
    End Class

    Figure 2

    Imports System.IO
    
    Module Module1
    
        Private FileName As String = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "users.txt")
        Private userList As List(Of User)
    
    
        Sub Main()
            If Not File.Exists(FileName) Then
                Exit Sub
            End If
    
            GetUsers()
    
            Console.ForegroundColor = ConsoleColor.Cyan
    
            Console.WriteLine("Login")
            Console.ForegroundColor = ConsoleColor.White
    
            Dim userLoginName = ""
            Dim loginAttempts As Integer = 0
    
            'Simple iteration upto three times
            For index As Integer = 0 To 2
    
                Console.Write("Enter username: ")
                Dim username As String = Console.ReadLine()
                Console.Write("Enter password: ")
                Dim password As String = Console.ReadLine()
    
                Dim foundUser = userList.FirstOrDefault(
                    Function(user) user.Name = username AndAlso user.Password = password)
    
                If foundUser Is Nothing Then
                    loginAttempts += 1
                    Console.Clear()
                Else
                    userLoginName = username
                    Exit For
                End If
            Next index
    
            Console.Clear()
    
            'Display the result
            If loginAttempts > 2 Then
                Console.WriteLine("Login failure")
            Else
                Console.WriteLine($"Login successful for {userLoginName}")
            End If
    
    
            Console.ReadKey()
    
        End Sub
    
    
        Public Sub GetUsers()
            userList = New List(Of User)
            Dim lines = File.ReadAllLines(FileName)
    
            For Each line As String In lines
                Dim parts = line.Split(","c)
                userList.Add(New User() With {.Name = parts(0), .Password = parts(1)})
            Next
        End Sub
    
    End Module
    Public Class User
        Public Property Name() As String
        Public Property Password() As String
    End Class

    Edit: This one hides the password.

    Imports System.IO
    
    Module Module1
    
        Private FileName As String = Path.Combine(
            AppDomain.CurrentDomain.BaseDirectory, "users.txt")
    
        Private userList As List(Of User)
    
    
        Sub Main()
            If Not File.Exists(FileName) Then
                Exit Sub
            End If
    
            GetUsers()
    
            Console.ForegroundColor = ConsoleColor.Cyan
    
            Console.WriteLine("Login")
            Console.ForegroundColor = ConsoleColor.White
    
            Dim userLoginName = ""
            Dim loginAttempts As Integer = 0
    
            'Simple iteration up to three times
            For index As Integer = 0 To 2
    
                Console.Write("Enter username: ")
                Dim username As String = Console.ReadLine()
                Console.Write("Enter password: ")
                Dim password As String = ReadPassword()
    
                Dim foundUser = userList.FirstOrDefault(
                    Function(user) user.Name = username AndAlso user.Password = password)
    
                If foundUser Is Nothing Then
                    loginAttempts += 1
                    Console.Clear()
                Else
                    userLoginName = username
                    Exit For
                End If
            Next index
    
            Console.Clear()
    
            'Display the result
            If loginAttempts > 2 Then
                Console.WriteLine("Login failure")
            Else
                Console.WriteLine($"Login successful for {userLoginName}")
            End If
    
    
            Console.ReadKey()
    
        End Sub
    
    
        Public Sub GetUsers()
            userList = New List(Of User)
            Dim lines = File.ReadAllLines(FileName)
    
            For Each line As String In lines
                Dim parts = line.Split(","c)
                userList.Add(New User() With {.Name = parts(0), .Password = parts(1)})
            Next
        End Sub
        Public Function ReadPassword() As String
            Dim password As String = ""
            Dim info As ConsoleKeyInfo = Console.ReadKey(True)
            Do While info.Key <> ConsoleKey.Enter
                If info.Key <> ConsoleKey.Backspace Then
                    Console.Write("*")
                    password &= info.KeyChar
                ElseIf info.Key = ConsoleKey.Backspace Then
                    If Not String.IsNullOrEmpty(password) Then
                        password = password.Substring(0, password.Length - 1)
                        Dim pos As Integer = Console.CursorLeft
                        Console.SetCursorPosition(pos - 1, Console.CursorTop)
                        Console.Write(" ")
                        Console.SetCursorPosition(pos - 1, Console.CursorTop)
                    End If
                End If
                info = Console.ReadKey(True)
            Loop
            Console.WriteLine()
            Return password
        End Function
    End Module
    Public Class User
        Public Property Name() As String
        Public Property Password() As String
    End Class

    File used

    paul,p1
    karen,paw1
    nick,1234


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange


    Tuesday, October 15, 2019 7:52 PM
    Moderator
  • Hi,

    Have you solved this problem now?
    I think the above reply can provide you with a solution, have you tried it?
    If so, hope you can close this thread by marking the reply as answer as this will help others looking for the same or similar issues down the road.
    Best Regards,

    Julie


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, October 16, 2019 5:24 AM
    Moderator