none
Saving User name and Password in Visual Basic RRS feed

  • Question

  • Hi, I wanted to know if it is possible to save the Username and Password of a users input. So in the code below, I created two variables called username and password, I prompt the user to make a Username and Password, but, I do not want to prompt the user every time they start the program to create a Username and Password, is there any way for me to store their Username and Password so that the next time they start the program, it will not prompt them for a user name and password, but it will just go to enter you Username and Password

    Public Class Form1
        Dim username As String
        Dim password As String
        Private Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click
            'username = InputBox("What do you want your Username to be?")
            'password = InputBox("What do you want your Password to be?")
            If TextBox1.Text = username And TextBox2.Text = password Then
                MsgBox("You are logged in ")
                Form2.Show()
            Else
                MsgBox("Sorry Wrong Username and or Password")
            End If
        End Sub

        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            username = InputBox("What do you want your Username to be?")
            password = InputBox("What do you want your Password to be?")
        End Sub
    End Class

    Saturday, September 29, 2018 1:31 PM

Answers

  • Hi,

    After you have successfully logged in, save the information in an XML file, and then open the login window and read it.

    Best Regards,

    Alex


    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.


    • Edited by Alex Li-MSFT Saturday, September 29, 2018 2:12 PM
    • Marked as answer by Frozen_Nova Saturday, September 29, 2018 3:36 PM
    Saturday, September 29, 2018 2:11 PM
  • Hi

    Please use the code block tool to post code (second from right on Toolbar)

    *

    If your application does NOT depend on absolute security, and the User Name and Passord are NOT super secret then:

    One way is to set up two My.Settings variables (lets call them UserName and PassWord).

    To set these up, open Solution Explorer and doubleclick on the 'My Project', then in the dialog, tabs to the left, open Settings.

    In the Setting dialog pane, in the Name column, type in 'UserName', then in the Type column, from the drop down, select String and in the Value column, set a meaningfull Name that you will use later on  (lets call it NotRegistered)

    Repeat the above for the PassWord variable.

    In both of those, leave the Scope column as the default User

    *

    Now you have these items set up, you can reference these within your code as you would any variable. The syntax used is My.Settings.XXXXX where XXXXX is the name of the variable you set up. eg  My.Settings.UserName  or  My.Settings.Password

    *

    In your code, you can check the variable(s) and find if they still hold the default value you set them up with, for example, if either of them returns NotRegistered, then you would know the User needs to register their UserName and Password. If these variables do NOT return NotRegistered then you will know that they have already entered their UserName and/or Password.

    *

    In code, use these variables as you would any other variable. As they are String types, you can, for example, say

    TextBox22.Text = My.Settings.UserName,     or,

    My.Settings.UserName = TextBox22.Text

    *

    The default behaviour is that these settings are saved automatically, so they will hold their values between sessions without having to write code to output to (say) a text file or whatever.


    Regards Les, Livingston, Scotland



    • Edited by leshay Saturday, September 29, 2018 2:21 PM
    • Marked as answer by Frozen_Nova Saturday, September 29, 2018 2:41 PM
    Saturday, September 29, 2018 2:18 PM

All replies

  • Hi,

    After you have successfully logged in, save the information in an XML file, and then open the login window and read it.

    Best Regards,

    Alex


    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.


    • Edited by Alex Li-MSFT Saturday, September 29, 2018 2:12 PM
    • Marked as answer by Frozen_Nova Saturday, September 29, 2018 3:36 PM
    Saturday, September 29, 2018 2:11 PM
  • Hi

    Please use the code block tool to post code (second from right on Toolbar)

    *

    If your application does NOT depend on absolute security, and the User Name and Passord are NOT super secret then:

    One way is to set up two My.Settings variables (lets call them UserName and PassWord).

    To set these up, open Solution Explorer and doubleclick on the 'My Project', then in the dialog, tabs to the left, open Settings.

    In the Setting dialog pane, in the Name column, type in 'UserName', then in the Type column, from the drop down, select String and in the Value column, set a meaningfull Name that you will use later on  (lets call it NotRegistered)

    Repeat the above for the PassWord variable.

    In both of those, leave the Scope column as the default User

    *

    Now you have these items set up, you can reference these within your code as you would any variable. The syntax used is My.Settings.XXXXX where XXXXX is the name of the variable you set up. eg  My.Settings.UserName  or  My.Settings.Password

    *

    In your code, you can check the variable(s) and find if they still hold the default value you set them up with, for example, if either of them returns NotRegistered, then you would know the User needs to register their UserName and Password. If these variables do NOT return NotRegistered then you will know that they have already entered their UserName and/or Password.

    *

    In code, use these variables as you would any other variable. As they are String types, you can, for example, say

    TextBox22.Text = My.Settings.UserName,     or,

    My.Settings.UserName = TextBox22.Text

    *

    The default behaviour is that these settings are saved automatically, so they will hold their values between sessions without having to write code to output to (say) a text file or whatever.


    Regards Les, Livingston, Scotland



    • Edited by leshay Saturday, September 29, 2018 2:21 PM
    • Marked as answer by Frozen_Nova Saturday, September 29, 2018 2:41 PM
    Saturday, September 29, 2018 2:18 PM
  • Thanks so much, this was very helpful and well written 
    Saturday, September 29, 2018 3:33 PM