Hey guys,
Per a request from a customer I created a new UserManagement component of my own. There are plenty of tutorials out there on how to do this in VB.net but for those who aren't code savvy I thought I would share mine and offer any help I can to those who reply to this thread.
First off here is what it looks like on MY user interface.
ScreenShots:Features:- Login/Logout function
- Local .csv database of users and passwords
- 3 Levels of privileges Operator, Supervisor, Administrator
- Passwords are stored as Encrypted strings
- Public events for Login Logout each page can subscribe to
Usage:Start by adding the file(s) to your project. Right click on your project name and select "Add --> Existing Item"
Please make sure to select all of the files in the .zip which were including as an attachment in this post.
Login:Next somewhere on your project add a Button control to your form and double-click on it to add the handler for the Click event and show the related code.
The is a function in the UserManagement Class called "Login" this function when called for the very first time ever on a computer will check if the local database file (.csv) exists yet on the computer. If it does not then the function will create it and also add the "Admin" user to the database. The "Admin" user has the default login of User:
Admin Pass:
password. Note that the Username is NOT case sensitive but the Password however is.
Add a call to this Login function using code similar to this.
Private Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click
frmLogin.ShowDialog()
If Not UserManagement.currentUser = "" Then
'Do Something here to handle the login succesfull... maybe update a label.text control with the UserManagement.currentUser.
'There will also be a UserManagement.currentPrivilage which will be set based on the credentials of the user which logged in.
End If
End Sub
When the function is called it will display the Login Dialog. The Login Dialog will handle the call to the login function within the UserManagement.vb. When the login is successful the function will set 2 shared variables in the UserManagement class for the "currentUser" and "currentPrivilage". It will also fire a Shared Event for the Login.
It is recommended that you have each form which will dynamically be changing based on the User logged in and their Privileges to subscribe to the public events of "userLoggedIn" and "userLoggedOut". Here is how to handle those events.
In the Load event for the Form in question, add a handler for each event.
AddHandler UserManagement.userLoggedIn, AddressOf handleUserLoggedIn
AddHandler UserManagement.userLoggedOut, AddressOf handlerUserLoggedOut
Next create the Subs for each of those events or you can copy this code and add it to your own.
Private Sub handleUserLoggedIn()
If UserManagement.currentPrivilage = UserManagement.Privilage.Administrator Or UserManagement.currentPrivilage = UserManagement.Privilage.Supervisor Then
Me.tbManualVinEntry.Enabled = True
End If
End Sub
Private Sub handlerUserLoggedOut()
If Not UserManagement.currentPrivilage = UserManagement.Privilage.Administrator Or Not UserManagement.currentPrivilage = UserManagement.Privilage.Supervisor Then
Me.tbManualVinEntry.Enabled = False
End If
End Sub
In these examples I am checking the value of the privilege for the current user who is logged in. That is stored in the UserManagement.currentPrivilage variable. Depending on the privilege of the user you can create different methods to turn On or OFF various user controls on the form. Each form can subscribe to these events and alter the controls within accordingly.
Logout:Finally back on the main form where the Login button resides. Add another button for the Logout functionality. Then in the Click event of the button add the following code.
Dim myUserManagement As New UserManagement
Call myUserManagement.Logout()
Adding a User:To add a user, somewhere in you project create a button to add a user. Double click on this button to access the click event code. Add the following code.
Private Sub btnAddNewUser_Click(sender As Object, e As EventArgs) Handles btnAddNewUser.Click
frmAddUser.ShowDialog()
End Sub
This will open up the frmAddUser so you can properly add a user to the database. NOTE: It is suggested that the visibility property of this button be set to False by default and only enabled if the current user logged in has a Privilege of Administrator. That can be handled by reading the Login section above.
The addUser function will check if the username already exists in the database and if so will prompt if you would like to overwrite it. This gives you the ability to just change the password easily if needed. After you click "Add User" the function will display a messagebox to inform you if it was a success or a failure.
Deleteing a User:In my example there is a combo box which I populate on the load event of the form of all of the users in the database. To do this add a ComboBox
To do this, add a Combo Box control to your form, set the Name property to "cboUserList" then open up the Load Event logic of the form. Add the following code (make sure you change the name of the Combo Box control to match your project if needed)
Me.cboUserList.Items.Clear()
Dim userList As New List(Of String)
Dim _userManagement As New UserManagement
_userManagement.getUserList(userList)
If Not userList.Count = 0 Then
For i = 0 To userList.Count - 1
Me.cboUserList.Items.Add(userList.Item(i).ToString)
Next
End If
Next add a new button control to your form and set the Text property to "Remove User". Double click on the button to open the click event code. Add the following code to yours keeping in mind the name of the Combo Box from the previous step.
Private Sub btnDeleteSelectedUser_Click(sender As Object, e As EventArgs) Handles btnDeleteSelectedUser.Click
If Not Me.cboUserList.Text = Nothing Then
Dim _userManagement As New UserManagement
_userManagement.deleteExistingUser(Me.cboUserList.Text)
Dim i As Integer = Me.cboUserList.SelectedIndex
Me.cboUserList.Items.RemoveAt(i)
End If
End Sub
This function will find the Username in the database and remove it. The comboBox will also remove the item from the list.
Please let me know if you have any questions below or of something isn't clear and I will modify this post and elaborate more.
Thanks! and enjoy.