How can I interpret a masking system in my login system?

纵然是瞬间 提交于 2019-12-31 04:17:07

问题


I'm just not sure how I can mask with the addition of arrays. I would also like the masking system to recognize backspace not as a letter.

Module Module1

Sub Main()

    Dim memofs As Char
    Dim stdntpassword As String = Nothing
    Dim staffpassword As String = Nothing
    Console.Write("Are you a member of staff? (y/n) ")
    memofs = Console.ReadLine
    If memofs = "y" Or memofs = "Y" Then

    ElseIf memofs = "n" Or memofs = "N" Then
        Console.WriteLine("Password: ")
        stdntpassword = Console.ReadLine
    End If

    For attempts As Integer = 1 To 5

        Console.WriteLine()
        Console.WriteLine("Attempt Number " & attempts)
        Console.WriteLine()
        Console.Write("Password: ")
        staffpassword = Console.ReadLine
        Dim fullline As String = ""
        FileOpen(1, _
          "E:\Computing\Spelling Bee\StaffPasswords\staffpassword.csv", OpenMode.Input)
        fullline = LineInput(1)
        Dim item() As String = Split(fullline, ",")
        If staffpassword = item(0) Then
            Console.Clear()
            staffmenu()
        Else : FileClose(1)
        End If
        Console.Clear()

Appreciated,


回答1:


I've done this once before, but instead of masking the password with, say an asterisk, I just left it blank (like entering a password in Linux). The latter is pretty straightforward:

Dim keyInfo as ConsoleKeyInfo = Console.ReadKey(True)
Dim enteredPassword as String = ""
' Read each entered character until user presses Enter.
While keyInfo.Key <> ConsoleKey.Enter
   If keyInfo.Key = ConsoleKey.Backspace AndAlso enteredPassword.Length > 0 Then
       enteredPassword = enteredPassword.Substring(0, enteredPassword.Length - 1)
   Else
       password &= keyInfo.KeyChar
   End If
   ' Read next entered character
   keyInfo = Console.ReadKey(True)
End While

To actually mask the password input, use the same idea, but after each character is entered and parsed, add a Console.Write("*"c). This get's a bit tricky with backspace, and the only way I know to simulate it would be to do:

Console.Write(ControlChars.Back)' move cursor back one column
Console.Write(" "c) ' clear the asterisk
Console.Write(ControlChars.Back)' move cursor back again to allow writing.

It's a bit prettier in C# in my opinion (and works using '\b' instead of ControlChars.Back) so your results may vary.

Also if there's an easier way to do this, I'd love to know, as this seems like reinventing the wheel for a fairly simple task.




回答2:


This post has a good working example for you. It has masking with backspace and redo support.



来源:https://stackoverflow.com/questions/20248057/how-can-i-interpret-a-masking-system-in-my-login-system

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!