Reading in Login info from outside

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ldpfrog
    New Member
    • Nov 2006
    • 16

    Reading in Login info from outside

    I am currently writing a very simple encryption program for my friends and myself. I'm still learning VB, but am very familiar with programming itself. This is all irrelevant though, because this technique could be applied to any program which requires logging in.

    I would like to be able to make a file (just a basic text file or spreadsheet to start would be fine) with Usernames and Passwords listed which will be accepted into my program. I currently just use If statements for two or three users, but will eventually probably distribute this to more of my friends, in which it would be much easier to host some sort of file for the program to connect to and read from.

    I understand how to use the Login information, I am just not familiar with reading in information with VB. Once that is working I'd also like a suggestion as to how I could make it so only the program (or some sort of password protection) could access the login information file.

    If we get this far, I would also like to be able to host it (which I can do) and have my program read from it on the web, so I could just change the file from my computer and it would work with whoever is using the software.

    Thanks alot!
    Frog
  • Killer42
    Recognized Expert Expert
    • Oct 2006
    • 8429

    #2
    If you have a look at the "Index of articles" at the top of the VB forum, there are links to a couple of samples showing how to read a text file in VB.

    I don't know how you can restrict access to the file, but one simple precaution would be to store the passwords in their encrypted form. That way, if someone edits the file, unless they know your encryption method, the most they can do is to stop it working by messing up the stored details. If you encrypt the entire file contents, or at least include some sort of checksum, then your program could detect that it has been modified and refuse to run.

    Comment

    • ldpfrog
      New Member
      • Nov 2006
      • 16

      #3
      I did see that but I don't understand how to use it. It essentially hands you a chunk of code and doesn't tell you how to use it

      I have also been looking online and many are saying to use ADO, where you would use excel as opposed to a text file. I have been screwing around with this with no success =/.

      Comment

      • danp129
        Recognized Expert Contributor
        • Jul 2006
        • 323

        #4
        Originally posted by ldpfrog
        I did see that but I don't understand how to use it. It essentially hands you a chunk of code and doesn't tell you how to use it

        I have also been looking online and many are saying to use ADO, where you would use excel as opposed to a text file. I have been screwing around with this with no success =/.
        Here's the article's code with the sub renamed (read makes more sense than dump) and an example to call it, and also a comment in the code. If you actually test the code with a file, it will be more apparent what's going on.

        [CODE=vb]
        Private Sub Command1_Click( )
        ReadFile_V01 ("c:\test.tx t")
        End Sub


        Public Sub ReadFile_V01(By Val FileName As String)
        Dim FileNo As Long
        Dim LineNo As Long
        Dim LineText As String

        FileNo = FreeFile ' Get next available file number.

        Open FileName For Input Access Read Shared As #FileNo
        Do Until EOF(FileNo) ' Repeat until end of file...
        Line Input #FileNo, LineText ' Read a line from the file.


        'put your code here to deal with the current line of text
        LineNo = LineNo + 1
        Debug.Print Format(LineNo, "00000"); ": "; LineText



        DoEvents ' Allow Windows to handle other tasks.
        Loop
        Close #FileNo
        End Sub
        [/CODE]

        If you want the password file hosted on a website you can use something like this to download the file:

        [CODE=vb]Option Explicit
        Private Sub Command1_Click( )
        Dim sHTML
        sHTML = RequestText("ht tp://news.google.com/")

        msgbox sHTML

        End Sub

        Private Function RequestText(sUR L, Optional sMethod = "POST")
        'You may have caching issues using GET
        Dim XMLHTTP
        Set XMLHTTP = CreateObject("m icrosoft.XMLHTT P")
        sMethod = UCase(sMethod)
        XMLHTTP.Open sMethod, sURL, False
        XMLHTTP.send (Null) '"x=x"
        RequestText = XMLHTTP.respons eText
        Set XMLHTTP = Nothing
        End Function[/CODE]

        Comment

        • Killer42
          Recognized Expert Expert
          • Oct 2006
          • 8429

          #5
          Originally posted by ldpfrog
          I did see that but I don't understand how to use it. It essentially hands you a chunk of code and doesn't tell you how to use it

          I have also been looking online and many are saying to use ADO, where you would use excel as opposed to a text file. I have been screwing around with this with no success =/.
          Hm... ADO and Excel... interesting. I haven't seen it done, but that doesn't prove anything I suppose. I guess both Excel and Access have the advantage of supporting some security measures.

          As for the samples, yes they really are just code snippets for people to play with. We don't have anything much in the way of tutorials as yet. There are heaps of VB tutorials available on the web, though. However, let's assume you had a text file with a bunch of user IDs, each one followed on the next line by an encrypted password. Here's a sample I threw together which will prompt you for the name of the file then invoke a little "black box" function to return an array of user IDs and encrypted passwords from it.

          This is the complete code module for the function...
          [CODE=vb]Option Explicit

          Public Type UserStuff
          ID As String
          Psw As String
          End Type

          Public Function GetUserDetailsF romFile(ByVal FileName As String) As UserStuff()
          Dim FileNo As Long
          'Dim LineNo As Long
          Dim LineText As String
          Dim TempUser() As UserStuff
          Dim UserNo As Long
          'Dim TempPsw() As String

          FileNo = FreeFile ' Get next available file number.

          Open FileName For Input Access Read Shared As #FileNo
          Do Until EOF(FileNo) ' Repeat until end of file...
          UserNo = UserNo + 1
          ReDim Preserve TempUser(1 To UserNo)
          Line Input #FileNo, TempUser(UserNo ).ID
          Line Input #FileNo, TempUser(UserNo ).Psw
          DoEvents ' Allow Windows to handle other tasks.
          Loop
          Close #FileNo
          GetUserDetailsF romFile = TempUser
          End Function[/CODE]

          And here's the code in my sample form to test it out...[CODE=vb]Option Explicit
          Dim MyUserDetails() As UserStuff

          Private Sub Form_DblClick()
          MyUserDetails = GetUserDetailsF romFile(InputBo x("Enter name (including path) of file with users"))
          Dim I As Long
          For I = LBound(MyUserDe tails) To UBound(MyUserDe tails)
          With MyUserDetails(I )
          Debug.Print "UserID:"; .ID, "Psw:"; .Psw
          End With
          Next
          End Sub[/CODE]Just run it, double-click the form and away you go. It just prints the results to the immediate window.

          My sample file was just a text file I created in Notepad, containing these stupid values...
          User01
          %JFK ASEF:F$$W
          User02
          45v-98n3pytp4
          User03
          $%Y$%$K%JFK$$W

          Comment

          • ldpfrog
            New Member
            • Nov 2006
            • 16

            #6
            Okay, thank you all alot for your help. This was the code that I came up with. I Created an array UInfo(999, 2). The X portion of this array is the maximum amount of usernames and passwords I could set, I could increase the amount but I doubt I'll have over 1000 users using this program. The Y portion is set up so if Y=1 then you are looking at a username, and if Y=2 then it is the password for the corresponding username.

            In order for this to work, just create a multiline Textbox and a Command button, with the default VB6 names. I would be happy to explain to anybody who needs it because this is a very simple solution to having multiple usernames and passwords to your program without editing the actual code.


            [CODE=vb]Dim nFileNum As Integer, UInfo(999, 2) As String, sNextLine As String, lLineCount As Long, NumLine As Integer, LCount As Integer, UnPw As Integer

            Private Sub Command1_Click( )

            nFileNum = FreeFile


            Open "C:\Documen ts and Settings\Matt\M y Documents\testi ng.txt" For Input As nFileNum
            lLineCount = 1

            NumLine = 0
            LCount = 1
            UnPw = 0
            Do While Not EOF(nFileNum)
            Line Input #nFileNum, sText
            LCount = LCount + 1
            If LCount Mod 2 <> 0 Then UnPw = 2
            If LCount Mod 2 = 0 Then
            NumLine = NumLine + 1
            UnPw = 1
            End If
            UInfo(NumLine, UnPw) = sText

            Loop
            UInfo(0, 1) = "UN"
            UInfo(0, 2) = "PW"
            For x = 0 To LCount
            For y = 1 To 2
            Text1.Text = Text1 & UInfo(x, y) + " "
            Next y
            Text1.Text = Text1 & vbCrLf
            Next x

            Close nFileNum

            End Sub[/CODE]
            Last edited by Killer42; Jul 9 '07, 02:35 AM. Reason: Add =vb to CODE tag.

            Comment

            • Killer42
              Recognized Expert Expert
              • Oct 2006
              • 8429

              #7
              Glad to see you got it working.

              Just one piece of general advice I would like to pass on. When you have two mutually exclusive possbilities (such as in lines 17-18 of your code) you should normally code it with an Else clause, as it makes the intention much more obvious. For instance...
              [CODE=vb]If LCount Mod 2 <> 0 Then
              UnPw = 2
              Else
              NumLine = NumLine + 1
              UnPw = 1
              End If[/CODE]

              Comment

              • ldpfrog
                New Member
                • Nov 2006
                • 16

                #8
                I know but I learned programming in TrueBasic and am not used to using 0 in arrays so I was afraid it would screw things up (which it did anyway). But thank you!

                Comment

                • Killer42
                  Recognized Expert Expert
                  • Oct 2006
                  • 8429

                  #9
                  Originally posted by ldpfrog
                  I know but I learned programming in TrueBasic and am not used to using 0 in arrays so I was afraid it would screw things up (which it did anyway). But thank you!
                  I never use entry 0 in an array either. I hate zero-based arrays - they go completely against the way humans think, and try to force them to think like computers. They should be banned.

                  All I was talking about in this case is the structure of the IF tests. You are doing the equivalent of...
                  [CODE=vB]IF Condition Then
                  ' Blah blah
                  End If
                  If Not Condition Then
                  ' Blah blah
                  End If[/CODE]
                  In such a case, it's usually simpler to read if you code it as[CODE=vb]IF condition Then
                  ' Blah blah
                  Else
                  ' Blah blah
                  End If[/CODE]

                  This is largely a personal choice, of course.

                  Comment

                  • ldpfrog
                    New Member
                    • Nov 2006
                    • 16

                    #10
                    Understood, thank you! Check out my other post about reading the file from the internet?

                    Comment

                    Working...