Application Level Security System in VB.NET

Description

This is a common situation where one needs to restrict some user for accessing some menus and some operations like AddNew Record, Edit / Change Record, Delete Record etc. it is required for almost every application that runs in a multi-user environment.

To do this you need some tables to hold data and a form to manipulate that data according to your policy.

Follow the three steps given below to accomplish this task.

Step 1:

Create the following tables in your database or in a new database:

Security1.gif

Security2.gif

Security3.gif
 

Security4.gif

Step 2 :

Make a form like given below (Codes attached):

Security5.gif

Step 3

Add the following code into your mdiForm's or Form contains menus Load events.

Public Sub LoadMenu()
Try
Dim
da As New SqlDataAdapter
Dim ds As New DataSet
Dim ds1 As New DataSet
Dim strSql As String
Dim nParentId As Integer
Dim i As Integer
Dim tsm As ToolStripMenuItem
 
For Each tsm In frmMain.MenuStrip.Items
strSql = "select Menus.MenuID,Menus.MenuName from UserRights " _
            & "Inner Join Menus On Menus.MenuID=UserRights.MenuID " _
           & "Where userID=" & intUserId & " And Menus.MenuName='" & tsm.Name.ToString & "' and            
                Menus.programid=" & intProgramID

da = New SqlDataAdapter(strSql, conSecurity)
ds.Tables.Clear()
ds = Nothing
ds = New DataSet
da.Fill(ds)
If ds.Tables(0).Rows.Count > 0 Then
nParentId = ds.Tables(0).Rows(0).Item("Menuid")
tsm.Enabled = True
For  i = 0 To tsm.DropDown.Items.Count - 1
'MsgBox(tsm.DropDownItems(i).Text)
strSql = "select Menus.MenuID,Menus.MenuName from UserRights " _
& "Inner Join Menus On Menus.MenuID=UserRights.MenuID AND Menus.programid = UserRights.ProgramID " _
& " Where userID=" & intUserId & " And Menus.MenuName='" & tsm.DropDownItems(i).Name & "'"
da = New SqlDataAdapter(strSql, conSecurity)
ds1.Tables.Clear()
ds1 = Nothing
ds1 = New DataSet
da.Fill(ds1)
If ds1.Tables(0).Rows.Count > 0 Then
tsm.DropDownItems(i).Enabled = True
Else
tsm.DropDownItems(i).Enabled = False
End If
Next
Else
' tsm.Enabled = False
End If

Next

Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Information, "Loading Menus")
End Try
End Sub

Up Next
    Ebook Download
    View all
    Learn
    View all